Meaning of the string returned by NodeList.item(i).getChildNodes()

  • Thread starter Sébastien de Mapias
  • Start date
S

Sébastien de Mapias

Hi,
When I unmarshall an XML file to a node list (or rather,
parse it to an org.w3c.dom.Document to traverse all its
children), by doing the following:

~ Document doc = db.parse(xmlFile);
~ NodeList nl = doc.getChildNodes();
~ for (int i = 0; i<nl.getLength(); i++) {
~ NodeList lme = nl.item(i).getChildNodes();
~ System.out.print(lme);
~ ...

it always shows something like "[xmlElemName: null]".

I do not understand what this ": null" mean (always appears
so, whether my node contains a value and/or attributes or not).

Any idea ?

Thanks.
Regards,
SR
 
R

Roland de Ruiter

Hi,
When I unmarshall an XML file to a node list (or rather,
parse it to an org.w3c.dom.Document to traverse all its
children), by doing the following:

~ Document doc = db.parse(xmlFile);
~ NodeList nl = doc.getChildNodes();
~ for (int i = 0; i<nl.getLength(); i++) {
~ NodeList lme = nl.item(i).getChildNodes();
~ System.out.print(lme);
~ ...

it always shows something like "[xmlElemName: null]".

I do not understand what this ": null" mean (always appears
so, whether my node contains a value and/or attributes or not).

Any idea ?

Thanks.
Regards,
SR

What you see is the node's name being printed followed by its value. An
an element node (like <foo></foo> ) doesn't hold a value and therefor
the value null is printed.

On the other hand, for a text node or a comment node ( <!-- bar --> )
the value printed would be the actual text of that node. The names of
these nodes likely would be printed as "#text" and "#comment",
respectively.

See the list at
<http://java.sun.com/javase/6/docs/api/org/w3c/dom/Node.html>
 
M

Mike Schilling

Sébastien de Mapias said:
Hi,
When I unmarshall an XML file to a node list (or rather,
parse it to an org.w3c.dom.Document to traverse all its
children), by doing the following:

~ Document doc = db.parse(xmlFile);
~ NodeList nl = doc.getChildNodes();
~ for (int i = 0; i<nl.getLength(); i++) {
~ NodeList lme = nl.item(i).getChildNodes();
~ System.out.print(lme);
~ ...

it always shows something like "[xmlElemName: null]".

I do not understand what this ": null" mean (always appears
so, whether my node contains a value and/or attributes or not).

Any idea ?

The value of Node.toString() is implemenrtation-specific, so it might
return almost anything. Use getNodeType(), getNodeName(), and
getNodeValue() to see what you've really got.
 
G

GArlington

Hi,
When I unmarshall an XML file to a node list (or rather,
parse it to an org.w3c.dom.Document to traverse all its
children), by doing the following:

~ Document doc = db.parse(xmlFile);
~ NodeList nl = doc.getChildNodes();
~ for (int i = 0; i<nl.getLength(); i++) {
~ NodeList lme = nl.item(i).getChildNodes();
At any point nl.item(i) is a childNode of your doc ['head', ...,
'body'...]
so, nl.item(i).getChildNodes() is a LIST of childNodes of the above
node...
~ System.out.print(lme);
~ ...

it always shows something like "[xmlElemName: null]".

I do not understand what this ": null" mean
Current node does NOT have any child nodes (???)
 
S

Stanimir Stamenkov

it always shows something like "[xmlElemName: null]".

I do not understand what this ": null" mean (always appears
so, whether my node contains a value and/or attributes or not).

Any idea ?

The Apache Xerces implementation you seems to observe is:

/** NON-DOM method for debugging convenience. */
public String toString() {
return "["+getNodeName()+": "+getNodeValue()+"]";
}

And the nodeValue of Element nodes is always null:

http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1841493061

(see the table a bit down below)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top