• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing XML using DOMParser  RSS feed

 
Raj S Kumar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please help me in parsing the following XML snippet.
<Project>
<LevLanguages>
<lang>DEU</lang>
<lang>ESN</lang>
<lang>FRA</lang>
<lang>ITA</lang>
</LevLanguages>
</Project>

I want to get the values of the <lang> nodes.
I have tried with the following code and it give 'null' value.

Element docEl = doc.getDocumentElement();
NodeList nol = docEl.getElementsByTagName("LevLanguages");

Element levLang =(Element) nol.item(0);
NodeList lang = levLang.getElementsByTagName("lang");
System.out.println(lang.getLength());

if (lang != null && lang.getLength() > 0) {
for (int i = 0; i < lang.getLength(); i++) {

System.out.println("Language:"+((Element)lang.item(i)).getNodeValue());
}
}

Could someone please tell me what is wrong in this piece of code?

Thanks in Advance,
Raj S Kumar
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj S Kumar wrote:I have tried with the following code and it give 'null' value.

Hmm... Maybe TelltheDetails about what exactly is the problem with your code would make this easier. Using CodeTags would be great too.

you should use getTextContent() method to determine the text content of an element, not getNodeValue() method.

 
Raj S Kumar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Thanks for the reply. getTextContent() method works fine.

Thanks again for the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!