Question

I am going through JAXP DOM trail where there is a DOM Mixed Content model as shown below

<sentence>This is an <bold>important</bold> idea.</sentence>

It's been explained as below

Note - The DOM Node API defines nodeValue(), nodeType(), and nodeName() methods. For the first element node, nodeName() returns sentence, while nodeValue() returns null. For the first text node, nodeName() returns #text, and nodeValue() returns “This is an ”. The important point is that the value of an element is not the same as its content.

My question is why the getNodeValue() invoked on Element node returns null. What is it supposed to have? What is actually a value in Element node? If i want to set a value to an element node, how can I do that ? Please explain.

Était-ce utile?

La solution

The value of an Element node is always null by design. See the Node documentation for a list of expected values.

This ultimately comes from the W3C DOM level 3 design.

Autres conseils

Use Node.getChildNodes(). Mapping the "value" to the same child nodes, would have been a duplicity. It is an awkward API, to "set a value" one has to remove all children, and call appendChild or replace/insert.

Ugly, but w3c predates Java, and somehow it was nice, that Java respected w3c.org by adoption.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top