on 02-08-2007 7:27 AM
i have
[code]
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<Motor>
<MotorElement><platno>jacket</platno>
<type>blue</type>
</MotorElement>
<MotorElement><platno>skirt</platno>
<type>red</type>
</MotorElement>
<Motor>
[/code]
how do i output make it like
PlatNo Type
jacket blue
skirt red
according to the code
my problem is instead of creating each row, how do loop each record and start with a new rows....
for (int i = 0; i < nodeList.getLength(); i++) {
HSSFRow rowno=spreadSheet.createRow(i);
//loop each record
}
This code is not correct...
[code]
public static void generateExcel(File xmlDocument) {
try {// Creating a Workbook
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet spreadSheet = wb.createSheet("spreadSheet");
spreadSheet.setColumnWidth((short) 0, (short) (256 * 25));
spreadSheet.setColumnWidth((short) 1, (short) (256 * 25));
// Parsing XML Document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlDocument);
NodeList nodeList = document.getElementsByTagName("MotorElement");
// Creating Rows
System.out.println("create row");
HSSFRow row = spreadSheet.createRow(0);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("Plat No");
cell = row.createCell((short) 2);
cell.setCellValue("Type");
HSSFRow row1 = spreadSheet.createRow(1);
HSSFRow row2 = spreadSheet.createRow(2);
HSSFRow row3 = spreadSheet.createRow(3);
HSSFRow row4 = spreadSheet.createRow(4);
HSSFRow row5 = spreadSheet.createRow(5);
HSSFRow row6 = spreadSheet.createRow(6);
HSSFRow row7 = spreadSheet.createRow(7);
HSSFRow row8 = spreadSheet.createRow(8);
HSSFRow row9 = spreadSheet.createRow(9);
HSSFRow row10 = spreadSheet.createRow(10);
HSSFRow row11 = spreadSheet.createRow(11);
for (int i = 0; i < nodeList.getLength(); i++) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
cell = row1.createCell((short) 0);
cell.setCellValue("platno($)");
cell = row1.createCell((short) 1);
cell.setCellValue(((Element) (nodeList.item(0)))
.getElementsByTagName("platno").item(0)
.getFirstChild().getNodeValue());
cell = row2.createCell((short) 0);
cell.setCellValue("type ($)");
cell=row1.createCell((short)2);
cell.setCellValue(((Element) (nodeList.item(0)))
.getElementsByTagName("type").item(0)
.getFirstChild().getNodeValue());
cell = row2.createCell((short) 1);
cell.setCellValue(((Element) (nodeList.item(1)))
.getElementsByTagName("platno").item(0)
.getFirstChild().getNodeValue());
cell = row2.createCell((short) 2);
cell.setCellValue(((Element) (nodeList.item(1)))
.getElementsByTagName("type").item(0)
.getFirstChild().getNodeValue());
}
// Outputting to Excel spreadsheet
System.err.println("Outputing");
FileOutputStream output = new FileOutputStream(new File("c:
OutputDoc.xls"));
wb.write(output);
output.flush();
output.close();
} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
} catch (ParserConfigurationException e) {
System.out
.println("ParserConfigurationException " + e.getMessage());
} catch (SAXException e) {
System.out.println("SAXException " + e.getMessage());
}
}
}[/code]
Message was edited by:
yzme yzme
Message was edited by:
yzme yzme
What do you expect as value of this expression?
document.getElementsByTagName("Motor/MotorElement");
See
/**
* Returns a <code>NodeList</code> of all the <code>Elements</code> in
* document order with a given tag name and are contained in the
* document.
* @param tagname The name of the tag to match on. The special value "*"
* matches all tags. For XML, the <code>tagname</code> parameter is
* case-sensitive, otherwise it depends on the case-sensitivity of the
* markup language in use.
* @return A new <code>NodeList</code> object containing all the matched
* <code>Elements</code>.
*/
public NodeList getElementsByTagName(String tagname);
Armin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
<?xml version='1.0' encoding='UTF-8' standalone='no'?><Motor>
<MotorElements>
<MotorElement><platno>jacket</platno>
<type>blue</type>
</MotorElement>
<MotorElement><platno>met</platno>
<type>sddd</type></MotorElement>
<MotorElement><platno>gg</platno>
<type>sds</type></MotorElement>
<MotorElement><platno>met</platno>
<type>sdud</type></MotorElement>
</MotorElements>
</Motor>
<?xml version='1.0' encoding='UTF-8' standalone='no'?><Motor>
<MotorElement><platno>jacket</platno>
<type>blue</type>
</MotorElement>
<MotorElement><platno>met</platno>
<type>sddd</type></MotorElement>
<MotorElement><platno>gg</platno>
<type>sds</type></MotorElement>
<MotorElement><platno>met</platno>
<type>sdud</type>
</MotorElement>
</Motor>
if i add another <MotorElements></MotorElements> into it ....
and i use...
NodeList nodeList = document.getElementsByTagName("MotorElement/MotorElement");
it doesnt work....
NodeList nodeList = document.getElementsByTagName("MotorElement");
only work for xml without the </MotorElements>
how do i define the getElementsByTagName()
Hi,
The parsing should have been done like this:
NodeList listOfMotorElems = doc.getElementsByTagName("MotorElement");
for(int i = 0; i < listOfMotorElems.getLength(); i++){
Node motorNode = listOfMotorElems.item(i);
if(motorNode.getNodeType() == Node.ELEMENT_NODE){
Element motorElem = (Element)motorNode;
NodeList platNoList = motorElem.getElementsByTagName("platno");
Element platNoListElem = (Element)platNoList.item(0);
NodeList textFNList = platNoListElem.getChildNodes();
String platVal = ((Node)textFNList.item(0)).getNodeValue().trim());
//add platVal to row.
//Similarly for other elements.
}
}
Regards,
Satyajit.
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.