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

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

问题描述

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

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

我的 XML 就像

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

而且我正在尝试获取 tag 值(非文本元素获取工作正常).我的 Java 代码听起来像

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());
        }
    }
}

打印出来

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

感谢您的帮助.

推荐答案

我会打印出 an2.getNodeName() 的结果以及用于调试目的.我的猜测是您的树爬行代码没有爬到您认为的节点.由于没有检查代码中的节点名称,这种怀疑更加严重.

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.

除此之外,Node 的 javadoc 定义了getNodeValue()" 为 Element 类型的节点返回 null.因此,您确实应该使用 getTextContent().我不知道为什么那不会给你你想要的文字.

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">
" +
             "    <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());
        }
    }
}

输出是:

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

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

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