aspose file tools*
The moose likes XML and Related Technologies and the fly likes Parsing XML doc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parsing XML doc" Watch "Parsing XML doc" New topic
Author

Parsing XML doc

Mike Dev
Greenhorn

Joined: Jul 11, 2005
Posts: 11
Hi,

I have the following XML file:

<words>
<word>aforementioned</word>
<word>globalisation</word>
<word>internet</word>
<word>extranet</word>
<word>scriptlet</word>
<word>improper</word>
<word>sunset</word>
<word>alternative</word>
<word>degree</word>
<word>grade</word>
</words>

I tried many things, but nothing seems to work (getFirstChild, NextSibling, etc.). Can anyone help, please?

Thanks.
Mike
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Welcome to Javaranch Mike.

You may need some dom/sax tutorials first.Some good ones were http://www-128.ibm.com/developerworks/views/xml/libraryview.jsp?type_by=Tutorials , http://www.w3schools.com etc.,

Also if you just want to parse the xml and display it in html form, then try to learn xslt as well. (w3schools got good tutorial on xslt).


Spritle Software Blogs
Mike Dev
Greenhorn

Joined: Jul 11, 2005
Posts: 11
OK. The code I've written is the following:

NodeList words = doc.getElementsByTagName("words");
Node n = doc.getDocumentElement();
for (Node child = n.getFirstChild();
child != null;
child = child.getNextSibling())
{
System.out.println(child.getNodeName() +" = "+ child.getNodeValue());
}

However, the getNodeValue() always returns NULL. Is this the normal behavior? Should I use a different method?

Thanks.
Mike




Originally posted by Balaji Loganathan:
Welcome to Javaranch Mike.

You may need some dom/sax tutorials first.Some good ones were http://www-128.ibm.com/developerworks/views/xml/libraryview.jsp?type_by=Tutorials , http://www.w3schools.com etc.,

Also if you just want to parse the xml and display it in html form, then try to learn xslt as well. (w3schools got good tutorial on xslt).
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Try this it will work



The text under each element is a child node and not a sibling also the getNodeValue of an Element returns Null by defination not the text it contains.

IMO DOM is too complex try JDOM it will make life simple.

Regards,
Rajagopal
Mike Dev
Greenhorn

Joined: Jul 11, 2005
Posts: 11
Thanks. It worked perfectly.

Mike



Originally posted by Rajagopal Manohar:
Try this it will work



The text under each element is a child node and not a sibling also the getNodeValue of an Element returns Null by defination not the text it contains.

IMO DOM is too complex try JDOM it will make life simple.

Regards,
Rajagopal
 
Consider Paul's rocket mass heater.
 
subject: Parsing XML doc