解析 Java 的 XML。

huangapple go评论66阅读模式
英文:

parsing Xml for Java

问题

以下是翻译好的部分:

存在这样的XML文档:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <a/>
    <a/>
    <a/>
    <b>
        <c/>
        <d/>
        <c/>
        <d/>
        <c/>
        <d/>
    </b>
</root>

以下代码用于获取 "a" 节点的NodeList:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlfile);
doc.getDocumentElement().normalize();
NodeList aNodes = doc.getElementsByTagName("a");

但是,在Java中如何检索 "c" 节点的NodeList呢?
我尝试获取 "b" 节点的子节点,但是没有结果,getLength() = 0。

NodeList bNodes = doc.getElementsByTagName("b");
NodeList cNodes = bNodes.item(0).getChildNodes();
英文:

There exists XML document like this:
<?xml version="1.0" encoding="utf-8"?>

&lt;root&gt;
	&lt;a/&gt;
	&lt;a/&gt;
	&lt;a/&gt;
	&lt;b&gt;
		&lt;c/&gt;
			&lt;d/&gt;
		&lt;c/&gt;
			&lt;d/&gt;
		&lt;c/&gt;
			&lt;d/&gt;
	&lt;/b&gt;
&lt;/root&gt;

Following code is using to get "a" NodeList:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlfile);
doc.getDocumentElement().normalize();
NodeList aNodes = doc.getElementsByTagName(&quot;a&quot;);

But how in Java I can retrieve NodeList of "c"?
I tried to get nodes of "b" nodes but it have not results, getlength() = 0.

NodeList bNodes = doc.getElementsByTagName(&quot;b&quot;);
NodeList cNodes = bNodes.item(0).getChildNodes();

答案1

得分: 1

你可以像这样使用 doc.getElementsByTagName(&quot;c&quot;) 来获取它们。

NodeList cNodes = doc.getElementsByTagName(&quot;c&quot;);
for (int i = 0; i &lt; cNodes.getLength(); ++i)
    System.out.println(cNodes.item(i));

输出:

[c: null]
[c: null]
[c: null]

我也可以检索到 "b" 的 NodeList。

NodeList bNodes = doc.getElementsByTagName(&quot;b&quot;);
NodeList bChildren = bNodes.item(0).getChildNodes();
for (int i = 0; i &lt; bChildren.getLength(); ++i)
    System.out.println(bChildren.item(i));

输出:

[#text: 
        ]
[c: null]
[#text: 
            ]
[d: null]
[#text: 
        ]
[c: null]
[#text: 
            ]
[d: null]
[#text: 
        ]
[c: null]
[#text: 
            ]
[d: null]
[#text: 
    ]
英文:

You can get them by doc.getElementsByTagName(&quot;c&quot;) like this.

NodeList cNodes = doc.getElementsByTagName(&quot;c&quot;);
for (int i = 0; i &lt; cNodes.getLength(); ++i)
    System.out.println(cNodes.item(i));

output:

[c: null]
[c: null]
[c: null]

I can also retrieve NodeList of "b".

NodeList bNodes = doc.getElementsByTagName(&quot;b&quot;);
NodeList bChildren = bNodes.item(0).getChildNodes();
for (int i = 0; i &lt; bChildren.getLength(); ++i)
    System.out.println(bChildren.item(i));

output:

[#text: 
        ]
[c: null]
[#text: 
            ]
[d: null]
[#text: 
        ]
[c: null]
[#text: 
            ]
[d: null]
[#text: 
        ]
[c: null]
[#text: 
            ]
[d: null]
[#text: 
    ]

huangapple
  • 本文由 发表于 2020年8月7日 12:47:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63295387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定