• 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

Help with this DOM parsing issue.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing a issue with DOm parsing. I have the XML:
<?xml version="1.0"?>
<item>
<stock-field>
<name>pin-number</name>
<value>12345678901234</value>
<display>true</display>
</stock-field>
<stock-field>
<name>toll-free-number</name>
<value>1800121212121</value>
<display>true</display>
</stock-field>
</item>
I need to create a hashmap of all possible stock-field names and values. The displcay tag is not of much relevance.
I am using now the following code:
Element root = doc.getDocumentElement();
NodeList stocklist = root.getElementsByTagName("stock-field");
String text = "";
Node stockfield, fieldnode = null;
NodeList nodes = null;
for (int i=0; i< stocklist.getLength(); i++ ) {
stockfield = stocklist.item(i);
nodes = stockfield.getChildNodes();
for (int j=0; j < nodes.getLength(); j++ ) {
fieldnode = nodes.item(j);
text = fieldnode.getNodeValue();
System.out.println ("\n Name: "+fieldnode.getLocalName()+" Type: "+fieldnode.getNodeType());

if (fieldnode.getNodeType() == Node.TEXT_NODE){
System.out.println("Value: "+text);
}

}
}
But the value (text String var) is always null as in doesnot carry the value of the element..
The output for the System.out is always Value: 'blank'
what am i doing wrong?
[ March 10, 2003: Message edited by: Manu Anand ]
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read here. Pay attention to the node value of various type of nodes. Then, you will know why you got blank etc.
 
A tiny monkey bit me and I got tiny ads:
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