wood burning stoves*
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes parsing doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "parsing doubt" Watch "parsing doubt" New topic
Author

parsing doubt

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
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();
}

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
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
Christian Nicoll
Ranch Hand

Joined: Mar 09, 2008
Posts: 106
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


SCJP 5, SCJD 5, SCWCD 5, SCBCD 5, SCJDWS 5
My SCBCD-Notes - My Hello World Webservice
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
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
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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

Joined: May 19, 2005
Posts: 263
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
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
At first glance, the code looks like it should work fine; what's the problem with it?
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parsing doubt
 
Similar Threads
DTD Validation problem via Sax
Parsing String which comes as xml
Deleting the children from node - xml
reading web.xml by XPATH
SOAP message