Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem While Parsing XML

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic