• 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 XML using DOMParser

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,
Thanks for the reply. getTextContent() method works fine.

Thanks again for the help.
 
I don't like that guy. The tiny ad agrees with me.
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