我有以下xml
<map>
<textures>
<texture id="1" name="texturetest" />
<texture id="2" name="ground1" />
</textures>
<tiles standardheight="60" standardwidth="60">
<tile id="1" textureid="2">
<attributes>
<attribute name="slope" value="0" />
<attribute name="slopedirection" value="left" />
<attribute name="friction" value="3" />
<attribute name="traversable" value="true"/>
</attributes>
<boundingbox type="box" width="60" height="60" />
</tile>
</tiles>
<moveables>
<object id="1" name="player1" textureid="1" width="60" height="60">
<attributes>
<attribute name="score" value="0" />
</attributes>
<boundingbox type="box" width="60" height="60" />
</object>
</moveables>
<background>
<layer name="bg1" zdistance="30" />
</background>
<tilemap>
<tile tileid="1" x="0" y="200" />
<tile tileid="1" x="60" y="200" />
</tilemap>
<objectmap>
<object objectid="1" initial_x="0" initial_y="0" initial_speed="50" initial_direction="90" />
</objectmap>
</map>
我使用以下xpath来获取moveables / object /
NodeList nodes = (NodeList) _path.evaluate("//map//moveables/object", nodesdoc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
NodeList attList = (NodeList) _path.evaluate("//attribute", nodes.item(i), XPathConstants.NODESET);
for (int x = 0; x < attList.getLength(); x++) {
String name = attList.item(x).getAttributes().getNamedItem("name").getNodeValue();
String value = attList.item(x).getAttributes().getNamedItem("value").getNodeValue();
t.Attributes.put(name, value);
}
}
然而,这会循环遍历文档中的所有“属性”元素,而不仅仅是“对象”中的属性元素。
有人能说清楚为什么会这样吗?
此外,如果我调用nodes.item(i).getChildNodes();我得到一个空引用异常.....
答案 0 :(得分:0)
尝试在第二个XPath的开头添加一个点,表示Xpath意味着相对于当前nodes.item(i)
:
NodeList attList = (NodeList)_path.evaluate(".//attribute", nodes.item(i), XPathConstants.NODESET);