ByteBiter
ByteBiter

Reputation: 116

Difference between TextContent and Value of an Attribute in XML

Is there a difference between getTextContent() and getValue() of attributes?

In the following case it prints the same into the console. I already found out that getNodeValue() and getValue are the same (according to http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html#getNodeValue() ).

XML:

<Request w="4.2">

Code:

getString("Request", rootElement);

and

void printAtt(String tagName, Element element) {
        NodeList list = element.getElementsByTagName(tagName);
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            Element nodeElement = (Element) node;
            Attr attribute = nodeElement.getAttributeNode("w");
            System.out.println("ATTR NAME: " + attribute.getName());
            System.out.println("ATTR TEXT CONTENT: " + attribute.getTextContent());
            System.out.println("ATTR VALUE: " + attribute.getValue());
            System.out.println("ATTR NODE VALUE: " + attribute.getNodeValue());
        }
}

The output is:

ATTR NAME: w
ATTR TEXT CONTENT: 4.2
ATTR VALUE: 4.2
ATTR NODE VALUE: 4.2

Upvotes: 2

Views: 3317

Answers (1)

Buhake Sindi
Buhake Sindi

Reputation: 89169

There is a vital difference. getTextContent() will concatenate all text content of its node and its descendants (if any) and return the value while getNodeValue() will return the value of its current node.

Javadoc states, getNodeValue():

The value of this node, depending on its type; see the table above. When it is defined to be null, setting it has no effect, including if the node is read-only.

getTextContent():

This attribute returns the text content of this node and its descendants. When it is defined to be null, setting it has no effect. On setting, any possible children this node may have are removed and, if it the new string is not empty or null, replaced by a single Text node containing the string this attribute is set to. On getting, no serialization is performed, the returned string does not contain any markup. No whitespace normalization is performed and the returned string does not contain the white spaces in element content (see the attribute Text.isElementContentWhitespace). Similarly, on setting, no parsing is performed either, the input string is taken as pure textual content. The string returned is made of the text content of this node depending on its type.

For an ATTRIBUTE_NODE, the value of both functions are the same since an attribute has no descendant(s) hence why you are receiving similar results.

Upvotes: 5

Related Questions