Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with getting node value using DOM 1

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I am trying to use DOM in order to receive an XML's Node value using this syntax:
String theDate = currentNode.getFirstChild().getNodeValue();
Now, although the node value is not empty I recveive null. Why is that? By that way, when I get it's Name I receive the name, so why is the value null when I know it is not?
 
If you posted the XML you're parsing, we'd have a better context to understand your problem.

You'd also need to indicate which is the currentNode at the point in code the problem arises.
 
I've no idea why this is the case, but I've found Java DOM implementations to be a bit odd sometimes.

If :

String theDate = currentNode.getFirstChild().getNodeValue();

returns null, I bet that :

String theDate = currentNode.getNodeValue();

returns what you want ...

I usually have a helper method that attempts to work out which particular method to use - maybe you should do something similar !

--------------------------------------------------
Free Database Connection Pooling Software
 
The XML is at this format:
<TabSchumForDays>
- <OneDay>
<Date>20030210</Date>
<Counter>3</Counter>
<Amount>4359</Amount>
</OneDay>
- <OneDay>
<Date>20030211</Date>
<Counter>3</Counter>
<Amount>2805</Amount>
</OneDay>
- <OneDay>
<Date>20030212</Date>
<Counter>4</Counter>
<Amount>3740</Amount>
</OneDay>
......

And I receive that String theDate is null using this code:


DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(new File("C:\\test.xml"));

Node root = document.getDocumentElement();
NodeList childNodes = root.getChildNodes();
Node currentNode;

for (int i=0; i<childNodes.getLength(); i++)
{
currentNode = childNodes.item(i);
String theDate = currentNode.getFirstChild().getNodeValue();
}
 
Looking at the org.w3c.dom.Node interface, calling getNodeValue() on quite a few of the DOM node types will yield nulls. For textual content of tags, you have to ensure you're actually calling the getNodeValue on the text node itself.

Once the XML is posted and we can see the node in question, all may become clearer.

Tim
 
ahhh. You're node calling getNodeValue on the text element of Date.
Try:-
Code:
NodeList list = ((Element)root).getElementsByTagName("Date");
int entries = list.getLength();
for ( int i = 0; i < entries; i++ ){
  Node n = list.item(i);
  String theDate = n.getFirstChild().getNodeValue();
}
 
...I should have started that with "You're not calling getNodeValue() on the text child element of the Date element".
 
timw,

It is working now, can you please explain me why your code is working and mine doesn't?
 
Have a read of org.w3c.dom.Node.

The text marked-up by tags is actually held in the DOM as a Text node inside the Element node (which is your Date tag). You needed to go one level further down, since calling getNodeValue on an Element returns null.

Tim
 
So what if I understand correctly I should do 3 loops for each Tag I have? That doen't sound right...
 
DOM is used for random access of nodes and their values. If you need to acces data sequentially, then you should be using a SAX parser.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top