0
<?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++);
}

しかし、これは機能しません。名のみが表示され、2番目のループではエラーが表示されます。

そして2番目の質問:何が重要ですかgetChildCount()

4

1 に答える 1

0

getChild(int i)i 番目の子ノードを返しますが、必ずしも i 番目の子要素ではありません。返されたリストを使用getChildElements("location")して反復します。もう 1 つの方法は、ルート要素で XPath クエリを使用することです。

また、javadoc は非常に有益です: http://www.xom.nu/apidocs/

于 2011-04-27T09:38:02.030 に答える