我的 XML 树具有以下结构:
<building id="5" name="Barracks" hp="2000" >
<cost metal="100" wood = "300"></cost>
<unit id="1" name="Swordsman" hp="40" attack="3"/>
<tech id="1" name="Research Heavy Swordsman"/>
</building>
这是我用来访问它的代码:
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
int civId=0, id=0;
if(qName.equalsIgnoreCase("building"))
{
inBuilding = true;
building = true;
//Fetching the ID of TownCenter, we use it as a reference to fetch the child nodes.
id = Integer.parseInt(attributes.getValue("id"));
if(id==5)
{
XMLfetch.put("id", attributes.getValue("id"));
XMLfetch.put("name", attributes.getValue("name"));
XMLfetch.put("hp", attributes.getValue("hp"));
......
}
}
if(inBuilding && id==5){ //<----- Condition which matters
if(qName.equalsIgnoreCase("cost")){
Log.i("resources", "Wood "+attributes.getValue("wood"));
Log.i("resources", "MetaL "+attributes.getValue("metal"));
XMLfetch.put("costWood", attributes.getValue("wood"));
XMLfetch.put("costMetal", attributes.getValue("stone"));
}
}
}
// END ELEMENT here
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(inBuilding)
{
if(qName.equalsIgnoreCase("building")) {
building=false;
//inBuilding=false;
}
if(qName.equalsIgnoreCase("cost")){
inBuilding=false;
}
}
}
现在的问题是,我无法进入标签。我无法访问它的属性,什么都没有。如果我从我提到的行中删除“id==5”的条件,代码会从整个树中获取所有成本值。但是,当我提出条件时,它什么也得不到!:( ...
请帮忙。多谢!