Je ne peux pas extraire de valeur de texte avec Node.getNodeValue()
, Node.getFirstChild().getNodeValue()
ou avec Node.getTextContent()
.Obtention d'une valeur de texte de noeud XML avec Java DOM
Mon XML est comme
<add job="351">
<tag>foobar</tag>
<tag>foobar2</tag>
</add>
Et je suis en train d'obtenir valeur tag (élément non textuel aller chercher des œuvres bien). Mon code Java ressemble
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());
}
}
}
Il imprime
tag type (1):
tag1
tag1
tag1
null
#text type (3):
_blank line_
_blank line_
...
Merci pour l'aide.
Il serait utile que vous indiquiez clairement ce que la variable 'n' contient actuellement exactement, le Document ou le documentElement? – AnthonyWJones
J'ai ajouté 'n' déclaration partie – Emilio