Christian Meyer wrote:
> When I say node.getNodeName() it tells me the correct Name, 
> but NodeValue() just tells null. It shouldn't do that.

Actually, it should. 

The DOM specification has a nice table at the beginning that 
lists all of the node types and what values their getNodeType, 
getNodeName, and getNodeValue methods should return. Element 
node values are defined to *always* be null because their text 
value is stored as Text node children of the element. 

Therefore, you need to access the children of the element to 
get the text value of an element. But the good thing is that
the DOM spec requires XML processors to combine adjacent text
nodes into a single node when the tree is built.

Here's some code for you.

  StringBuffer str = new StringBuffer();
  Node child = node.getFirstChild();
  while (child != null) {
    if (child.getNodeType() == Node.TEXT_NODE) {
      str.append(child.getNodeValue());
    }
    child = child.getNextSibling();
  }
  String value = str.toString();

Please note that this takes care of the simplest case. Your
code should be written to handle CDATA sections and entity
references that contain text (and possibly more entity refs).
For example:

  <!ENTITY text1 'Hello'>
  <!ENTITY text2 '&text1;, World'>

  <foo>"&text2;<![CDATA["]]></foo>

Which creates the following DOM tree fragment:

  ELEMENT_NODE "foo", null {
    TEXT_NODE "#text", "\""
    ENTITY_REFERENCE_NODE "text2", null {
      ENTITY_REFERENCE_NODE "text1", null {
        TEXT_NODE "#text", "Hello"
      }
      TEXT_NODE "#text", ", World"
    }
    CDATA_SECTION_NODE "#cdata-section", "\""
  }

If you control the document generation, then the simple case
may work for you. However, if you want to be able to handle
the more complicated case, look into using the DOM iterators
and walkers defined as part of the DOM Level 2 Views module.
They will make your life much easier.

-- 
Andy Clark * IBM, TRL - Japan * [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to