File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Problem While Parsing XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Problem While Parsing XML" Watch "Problem While Parsing XML" New topic
Author

Problem While Parsing XML

Asha Antony
Greenhorn

Joined: Jun 01, 2006
Posts: 6
I have an Xml file I have to parse this XML.While parsing i faced an issue.
for parsing i am using this code

NodeList nodes1 = xmlDoc.getElementsByTagName("table");

for (int i=0;i< nodes1.getLength(); i++) {
Element titleElem =(Element)nodes1.item(i);
Attr type = titleElem.getAttributeNode("name");
Node childNode = type.getFirstChild();
System.out.println("table Name is: " + childNode.getNodeValue());
System.out.println("table NodeName is: " + childNode.ELEMENT_NODE);
NodeList nodes2 = xmlDoc.getElementsByTagName("field");
for (int j=0;j< nodes2.getLength();j++) {
Element titleElem1 =(Element)nodes2.item(j);
Attr type3 = titleElem1.getAttributeNode("Name");
Node childNode2 = type3.getFirstChild();
System.out.println("field Name is: " + childNode2.getNodeValue());
}
}

Then i got this results

table Name is: acc_credit_cnfg
table NodeName is: 1
field Name is: str_store_id
field Name is: str_merchant_id
field Name is: str_fnam
field Name is: str_lnam
table Name is: acc_gc_pymt
table NodeName is: 1
field Name is: str_store_id
field Name is: str_merchant_id
field Name is: str_fnam
field Name is: str_lnam



But this is wrong ...Bcoz i have to get the result like this.....
table Name is: acc_credit_cnfg
table NodeName is: 1
field Name is: str_store_id
field Name is: str_merchant_id

table Name is: acc_gc_pymt
table NodeName is: 1
field Name is: str_fnam
field Name is: str_lnam...For that i tried too many ways but all are fail, Any One pls help me to sove this issue.



Sample XML File is Attched With this

<?xml version="1.0" encoding="ISO-8859-1"?>
<parameters>
<param type="Input">
<table name ="acc_credit_cnfg">
<fields>
<field Name ="str_store_id" type ="varchar" enable ="Yes" key ="Yes" aliasName ="Store Id"/>
<field Name ="str_merchant_id" type ="varchar" enable ="Yes" key ="Yes" aliasName ="Merchant Id"/>
</fields>
</table>
<table name ="acc_gc_pymt">
<fields>
<field Name ="str_fnam" type ="varchar" enable ="Yes" key ="Yes" aliasName ="First Name"/>
<field Name ="str_lnam" type ="varchar" enable ="Yes" key ="Yes" aliasName ="Last Name"/>
<fields>
</table>
</param>
</parameters>
[ June 11, 2006: Message edited by: Bear Bibeault ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

This always starts at the root (xmlDoc) and gets all the "field" elements under it. If I understand your code correctly (which maybe I don't because it's rather hard to read the way you posted it) then you should try this instead:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem While Parsing XML