The org.w3c.dom package contains an interface 'Node', which has (among others) methods getNodeName() and getNodeValue(). Now, there is often some unclarity regarding what these methods actually return for different types of Nodes...
The best resource for resolving the answer is the official javadocs (http://java.sun.com/j2se/1.4.1/docs/api/index.html) for the Node interface. There's a table listing what the return value actually is for your particular Node type:
| nodeType | nodeName | nodeValue | attributes |
| Attr | name of attribute | value of attribute | null |
| CDATASection | " " | content of the CDATA Section | null |
| Comment | " " | content of the comment | null |
| Document | " " | null | null |
| DocumentFragment? | " " | null | null |
| DocumentType? | document type name | null | null |
| Element | tag name | null | NamedNodeMap? |
| Entity | entity name | null | null |
| EntityReference? | name of entity referenced | null | null |
| Notation | notation name | null | null |
| ProcessingInstruction? | target | entire content excluding the target | null |
| Text | " " | content of the text node | null |
Notice that the getNodeValue() method for an instance of org.w3c.dom.Element, "<elem>value</elem>" for example, does not return the value of its contents. Instead, you need to getChildNodes() and concatenate the values of the Text nodes (there may be more than one) in order to get the enclosing Element's value.
The following is a sample snippet returning the value (contents) of an Element node:
public static String getNodeValue(Node node) {
StringBuffer buf = new StringBuffer();
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node textChild = children.item(i);
if (textChild.getNodeType() != Node.TEXT_NODE) {
System.err.println("Mixed content! Skipping child element " + textChild.getNodeName());
continue;
}
buf.append(textChild.getNodeValue());
}
return buf.toString();
}
XmlFaq CategoryCodeSamples