使用Java DOM获取XML节点文本值 [英] Getting XML Node text value with Java DOM

查看:79
本文介绍了使用Java DOM获取XML节点文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用获取文本值Node.getNodeValue() Node.getFirstChild()。getNodeValue() Node.getTextContent()



我的XML就像

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

我正在尝试获取标签的值(非文本元素提取工作正常)。我的Java代码听起来像

  Document doc = db.parse(new File(args [0])); 
Node n = doc.getFirstChild();
NodeList nl = n.getChildNodes();
Node an,an2; (int i = 0; i< nl.getLength(); i ++){
an = nl.item(i);



if(an.getNodeType()== Node.ELEMENT_NODE){
NodeList nl2 = an.getChildNodes(); (int i2 = 0; 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());
}
}
}

打印出

 标签类型(1):
标签1
标签1
标签1
null
#text type(3):
_blank line_
_blank line_
...

感谢您的帮助。

解决方案

我打印出 an2.getNodeName()以及调试目的。我的猜测是,您的树爬行代码不会爬行到您认为的节点。由于缺乏对代码中节点名称的检查来增强这种怀疑。



除此之外,Node的javadoc定义了 getNodeValue ()返回null类型的节点元件。所以你真的应该使用getTextContent()。我不知道为什么这不会给你你想要的文本。



也许迭代你的标签节点的孩子,看看有什么类型? p>

尝试这段代码,它适用于我:

  String xml = < add job = \351\> \\\
+
< tag> foobar< / tag> \\\
+
< tag> foobar2< /标签> \\\
+
< / add>;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
文档doc = db.parse(bis);
Node n = doc.getFirstChild();
NodeList nl = n.getChildNodes();
Node an,an2; (int i = 0; i< nl.getLength(); i ++){
an = nl.item(i);


if(an.getNodeType()== Node.ELEMENT_NODE){
NodeList nl2 = an.getChildNodes(); (int i2 = 0; 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());
}
}
}

输出为:

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


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.

解决方案

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.

Other than that, the javadoc for Node defines "getNodeValue()" to return null for Nodes of type Element. Therefore, you really should be using getTextContent(). I'm not sure why that wouldn't give you the text that you want.

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\">\n" +
             "    <tag>foobar</tag>\n" +
             "    <tag>foobar2</tag>\n" +
             "</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

这篇关于使用Java DOM获取XML节点文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆