<?xml version="1.0" encoding="UTF-8"?>
<locations>
<location id="83">
<location_name>name</location_name>
<company_name>company a</company_name>
<machines>
<machine id="12">A</machine>
<machine id="312">B</machine>
</machines>
</location>
<location id="85">
<location_name>name</location_name>
<company_name>company c</company_name>
<machines>
<machine id="21">A</machine>
<machine id="45">B</machine>
</machines>
</location>
</locations>我正在使用XOM并尝试获取所有位置名称,但我不知道如何获取。
Element root = doc.getRootElement();
int i = 1;
Element child = (Element) root.getChild(i);
while(child != null)
{
System.out.println(child.getFirstChildElement("location_name").getValue());
child = (Element) root.getChild(i++);
}但是这不起作用,它只显示名字,在第二个循环中它显示错误。
第二个问题:什么是getChildCount()??
发布于 2011-04-27 17:38:02
getChild(int i)返回第i个子节点,不一定是第i个子元素。使用getChildElements("location")遍历返回的列表。另一种方法是在根元素上使用XPath查询。
另外,javadoc提供了相当多的信息:http://www.xom.nu/apidocs/。
https://stackoverflow.com/questions/5801199
复制相似问题