tags:

views:

529

answers:

3
    try {
        String data = "<a><b c='d' e='f'>0.15</b></a>";
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory
                .newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(data));
        Document document = documentBuilder.parse(is);

        NodeList nl = document.getElementsByTagName("b");
        Node n = (Node) nl.item(0);
        System.out.println(n.getNodeValue());
    } catch (Exception e) {
        System.out.println("Exception " + e);

    }

I am expecting it to print 0.15, but it prints null. Any Ideas?

Edit: This did the trick

        if (n.hasChildNodes())
            System.out.println(n.getFirstChild().getNodeValue());
        else 
            System.out.println(n.getNodeValue());
+1  A: 

Try extracting it from the Element rather than from the Node:

try {
    String data = "<a><b c='d' e='f'>0.15</b></a>";
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
            .newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory
            .newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(data));
    Document document = documentBuilder.parse(is);

    NodeList nl = document.getElementsByTagName("b");
    Element el = (Element) nl.item(0);
    Text elText = (Text) el.getFirstChild();
    String theValue = elText.getNodeValue();
    System.out.println(theValue);
} catch (Exception e) {
    System.out.println("Exception " + e);
}
karim79
@mkal - I just edited to output the nodevalue instead of the Text instance!
karim79
+2  A: 

That's because an element in fact has no nodeValue. Instead it has a text node as a child, which has the nodeValue you want.

In short, you'll want to getNodeValue() on the first child of the element node.

Sometimes the element contains multiple text nodes, as they do have a maximum size, in which case you'll need something á la this, from the page linked earlier:

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();
}
Sebastian P.
A: 
System.out.println(n.getFirstChild().getNodeValue());
adatapost