[英]Java xml parser loop
I have this xml file: 我有这个xml文件:
<root>
<application>
<interface />
<interface />
</application>
<datatransmit>
<interface />
</datatransmit>
</root>
What I am trying to do is first do a loop through the interfaces within the <application>
tags and then another loop through the interfaces withing the <datatransmit>
tags. 我想做的是首先通过
<application>
标记内的接口进行循环,然后通过带有<datatransmit>
标记的接口进行另一循环。
I tried this with this Java code: 我用以下Java代码尝试过此操作:
NodeList application = doc.getElementsByTagName("application");
for (int i = 0; i < application.getLength(); i++) {
NodeList interfaces = doc.getElementsByTagName("interface");
for (int j = 0; j < interfaces.getLength(); j++) {
do some stuff...
}
}
I noticed that with this loop, it goes through a loop of all the interface elements. 我注意到,通过此循环,它经历了所有接口元素的循环。 Not just the interfaces withing the
application
tags but also the interfaces within datatransmit
. 不仅仅是接口withing的
application
标签,但也内的接口datatransmit
。 What would be a way to solve this? 有什么办法可以解决这个问题?
See the javadoc here 在这里查看javadoc
getElementsByTagName()
returns all descendants , and since you call doc.getElementsByTagName()
you get all descendants of doc
which match the element name, rather than all descendants of your application
element getElementsByTagName()
返回所有后代 ,并且由于您调用doc.getElementsByTagName()
您将获得与元素名称匹配的所有doc
后代,而不是application
元素的所有后代
Nearly there. 就快到了。
Your problem is using doc
as your root again in: 您的问题是在以下位置再次使用
doc
作为根目录:
NodeList interfaces = doc.getElementsByTagName("interface");
Meaning it will search the whole document. 这意味着它将搜索整个文档。 Instead, you should use the
getElementsByTagName
method on the application Element, to limit the range of your search: 相反,您应该在应用程序元素上使用
getElementsByTagName
方法来限制搜索范围:
NodeList application = doc.getElementsByTagName("application");
for (int i = 0; i < application.getLength(); i++) {
Element applicationElement = (Element) application.item(i);
NodeList interfaces = applicationElement.getElementsByTagName("interface");
for (int j = 0; j < interfaces.getLength(); j++) {
do some stuff...
}
}
You need to get the list of application
nodes from here: 您需要从此处获取
application
节点列表:
for (int i = 0; i < application.getLength(); i++) {
Node appNode = application.item(i);
....
}
and inspect the name/tag of the node via getNodeName()
for the value interface
. 并通过
getNodeName()
检查值interface
的节点名称/标签。
That applies for getting interface
as a child of application
. 这适用于将
interface
作为application
的子级获取。 If interface
only occurs under application
, then you can skip the first step and simply do 如果
interface
仅在application
下出现,则可以跳过第一步,只需执行
NodeList interfaces = doc.getElementsByTagName("interface");
Perhaps a more concise/flexible solution would be to use XPath , with a path such as /root/application/interface
? 也许更简洁/灵活的解决方案是使用XPath以及
/root/application/interface
类的路径?
As you are lopping through all elements from document
you are getting all elements. 当您浏览
document
所有元素时,您将获得所有元素。
doc.getElementsByTagName("interface");
You should get elements from each application
tag object. 您应该从每个
application
标记对象中获取元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.