• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing XML doc

 
Mike Dev
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Mike Dev
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic