为什么我的 xml 文件中有 #text 节点? [英] Why are there #text nodes in my xml file?

查看:27
本文介绍了为什么我的 xml 文件中有 #text 节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个对 xml 文件进行 DOM 解析的 android 应用程序.我有一个看起来像这样的 xml 文件:

I'm making an android application that does DOM parsing on an xml file. I have an xml file that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<family>
    <grandparent>
        <parent1>
            <child1>Foo</child1>
            <child2>Bar</child2>
        </parent1>
        <parent2>
            <child1>Raz</child1>
            <child2>Mataz</child2>
        </parent2>
    </grandparent>  
</family>

如果我在它上面运行一个 dom 解析器,像这样:

If I run a dom parser on it, like this:

try {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        
    Document doc = builder.parse(input);
    doc.getDocumentElement().normalize();   //added in since the edit
    NodeList nodd = doc.getElementsByTagName("grandparent");
    for (int x = 0; x < nodd.getLength(); x++){
        Node node = nodd.item(x);
        NodeList nodes = node.getChildNodes();
        for(int y = 0; y < nodes.getLength(); y++){
            Node n = nodes.item(y);
            System.out.println(n.getNodeName());
        }
    }
}

我的应用程序打印出以下内容

My application prints out the following

07-20 18:24:28.395:INFO/System.out(491):#text

07-20 18:24:28.395: INFO/System.out(491): #text

07-20 18:24:28.395: INFO/System.out(491): parent1

07-20 18:24:28.395: INFO/System.out(491): parent1

07-20 18:24:28.395:INFO/System.out(491):#text

07-20 18:24:28.395: INFO/System.out(491): #text

07-20 18:24:28.395: INFO/System.out(491): parent2

07-20 18:24:28.395: INFO/System.out(491): parent2

07-20 18:24:28.395:INFO/System.out(491):#text

07-20 18:24:28.395: INFO/System.out(491): #text

我的问题是,那些#text 字段是什么,更重要的是,我如何摆脱它们?

My question is, what are those #text fields and more importantly, how do I get rid of them?

所以现在我知道它们是什么,我尝试将其标准化.我已更新代码以反映更改,但结果相同.

So now that I know what they are, I tried to normalize it. I have updated the code to reflect the changes, but same result.

推荐答案

这是空格(换行符、空格、制表符):)

It's whitespace (newlines, spaces, tabs) :)

这篇关于为什么我的 xml 文件中有 #text 节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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