Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java XML parsing using DOM to get nodevalue

Tags:

java

dom

xml

    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());
like image 503
kal Avatar asked Aug 18 '09 00:08

kal


People also ask

What is nodeValue in XML?

The nodeValue property is used to get the text value of a node. The getAttribute() method returns the value of an attribute.

How DOM parses an XML file?

Android DOM(Document Object Model) parser is a program that parses an XML document and extracts the required information from it. This parser uses an object-based approach for creating and parsing the XML files. In General, a DOM parser loads the XML file into the Android memory to parse the XML document.

What is the best way to parse XML in Java?

DOM Parser is the easiest java xml parser to learn. DOM parser loads the XML file into memory and we can traverse it node by node to parse the XML. DOM Parser is good for small files but when file size increases it performs slow and consumes more memory.

How can parsing the XML data using DOM and SAX?

The two common ways to parse an XML document are given below: DOM Parser: Parsing the document by loading all the content of the document and creating its hierarchical tree structure. SAX Parser: Parsing based on event-based triggers. It does not require the complete loading of content.


1 Answers

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();
}
like image 149
Sebastian Paaske Tørholm Avatar answered Oct 12 '22 11:10

Sebastian Paaske Tørholm