DOM Parsing Clarification

60 views Asked by At

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.

2

There are 2 answers

0
McDowell On BEST ANSWER

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.

0
Joop Eggen On

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.