• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

parsing doubt

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in parsing. I am trying to parse a file "Address.xml" it runs successfully, it prints tag and value but if i pass sample.xml it doesn't print tag value of ISBN

--Address.xml-------- it works----------------------
<?xml version="1.0" encoding="UTF-8"?>
<addr:address type="Billing"
xmlns:addr="http://www.Monson-Haefel.com">
<!-- The address follows standard U.S. postal format -->
<name>AMAZON.COM</name>
<street>1850 Mercer Drive</street>
<city>Macon</city>
<state>KY</state>
<zip>40511</zip>
</addr:address>


-------sample.xml------------dosn't work
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:mh="http://www.Monson-Haefel.com/jwsbook/BookQuote">
<soap:Body>
<getBookPrice>
<isbn>0321146182</isbn>
</getBookPrice>
</soap:Body>
</soap:Envelope>

--------------------- java code----------- i am missing another loop to go isbn tag but i tried that also not working-----------

DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
try {
docBuilder = factory.newDocumentBuilder();
try {
Document doc=docBuilder.parse("sample.xml");
Element rootElement=doc.getDocumentElement();
// Output the elements contained by the root element.
NodeList nodeList = rootElement.getChildNodes();
for(int i=0;i < nodeList.getLength();i++){
Node node= nodeList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
String tagName = node.getNodeName();
System.out.println("tagname "+tagName);

NodeList nodeList2 = node.getChildNodes();
for(int j=0; j < nodeList2.getLength();j++){
Node text = (Node) nodeList2.item(j);
System.out.println("text "+text.getNodeValue());
}
}
}

} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do so.

Your code only examines nodes one level deep; that is "name" in the first example, and "SOAP:body" in the second example. "isbn" is two levels further down than "SOAP:body", though.
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your time but as i said above before the java code that piece is missing but i did tht and it doesn't work

i am missing another loop to go isbn tag but i tried that also not working
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitin,

when you want to parse an XML document then it is really error-prone to do this with two, three or further loops. If you want an generic solution and you never know how deep is your structure then you 've to write an generic function which is based recursion.

Best regards,
Christian
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for input Christian , i was expecting this reply on this code. Actually i copied this example from RMH, so i followed this without modifying the actual code. I know i can use recursive method to do so.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i am missing another loop to go isbn tag but i tried that also not working


Let me see if I understand this: you know that the code you posted can't possibly work, which is why you wrote some other code (that you didn't post) which tries to address the problem. Now that code also doesn't work (for reasons you don't understand) but you're asking for help by posting code that you fully understand can't work, and which isn't the code you're working on to solve this. Is that about right?
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before somebody else bomb me let me put another code is actually the code not working. It prints the first tag of "sample.xml" which is "soap:body" and rest it prints "text#"

Once again thanks for your time in looking this post and helping me out.


 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At first glance, the code looks like it should work fine; what's the problem with it?
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is output of above code. Shouldn't i got isbn number from sample.xml here? if not then what is the reason that is all my question is


tagname soap:Body
text

text null
text1

text1 null
text1 #text
text1

text

 
Police line, do not cross. Well, this tiny ad can go through:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic