Getting XML Node text value with Java DOM

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I can t fetch text value with Node.getNodeValue(), Node.getFirstChild().getNodeValue() or with Node.getTextContent().

My XML is like

<add job="351">
    <tag>foobar</tag>
    <tag>foobar2</tag>
</add>

And I m trying to get tag value (non-text element fetching works fine). My Java code sounds like

Document doc = db.parse(new File(args[0]));
Node n = doc.getFirstChild();
NodeList nl = n.getChildNodes();   
Node an,an2;

for (int i=0; i < nl.getLength(); i++) {
    an = nl.item(i);

    if(an.getNodeType()==Node.ELEMENT_NODE) {
        NodeList nl2 = an.getChildNodes();

        for(int i2=0; i2<nl2.getLength(); i2++) {
            an2 = nl2.item(i2);

            // DEBUG PRINTS
            System.out.println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):");

            if(an2.hasChildNodes())
                System.out.println(an2.getFirstChild().getTextContent());

            if(an2.hasChildNodes())
                System.out.println(an2.getFirstChild().getNodeValue());

            System.out.println(an2.getTextContent());
            System.out.println(an2.getNodeValue());
        }
    }
}

It prints out

tag type (1): 
tag1
tag1
tag1
null
#text type (3):
_blank line_
_blank line_
...

Thanks for the help.

Answers

I d print out the result of an2.getNodeName() as well for debugging purposes. My guess is that your tree crawling code isn t crawling to the nodes that you think it is. That suspicion is enhanced by the lack of checking for node names in your code.

http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/Node.html#getNodeValue() http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/Node.html#getNodeValue()

Perhaps iterate the children of your tag node and see what types are there?

Tried this code and it works for me:

String xml = "<add job="351">
" +
             "    <tag>foobar</tag>
" +
             "    <tag>foobar2</tag>
" +
             "</add>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
Document doc = db.parse(bis);
Node n = doc.getFirstChild();
NodeList nl = n.getChildNodes();
Node an,an2;

for (int i=0; i < nl.getLength(); i++) {
    an = nl.item(i);
    if(an.getNodeType()==Node.ELEMENT_NODE) {
        NodeList nl2 = an.getChildNodes();

        for(int i2=0; i2<nl2.getLength(); i2++) {
            an2 = nl2.item(i2);
            // DEBUG PRINTS
            System.out.println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):");
            if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getTextContent());
            if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getNodeValue());
            System.out.println(an2.getTextContent());
            System.out.println(an2.getNodeValue());
        }
    }
}

Output was:

#text: type (3): foobar foobar
#text: type (3): foobar2 foobar2

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/773012/getting-xml-node-text-value-with-java-dom

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils