I created an XML file and put it in my “res” folder. I decided to use the XmlResourceParser to parse the XML file.
My question is, how do I move to the next level of elements? Specifically, how can I access the child nodes?
Thanks
My XML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<encounters>
<creature id="1" type="humanoid" name="Jon" race="Human" gender="M" age="20" alignment="NE">
<appearance condition="ragged" skinColor="white" hairColor="brown" size="M" height="70" weight="170"></appearance>
<stats hp="20" ac="5" ap="5" mp="10" str="11" dex="15" int="15"></stats>
<inventory wp1="sword" wp2="knife" arm1="leather" arm2="bracers"></inventory>
<magic attSp1="fireball" attSp2="iceBall" defSp1="minor Shield" defSp2="major Shield"></magic>
<treasureItems gp="10" sp="25" item1="ring of protection + 1"></treasureItems>
</creature>
</encounters>
My code so far looks like this:
// ***** BEGIN XML PARSING *****
try {
while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
if (xrp.getEventType() == XmlResourceParser.START_TAG) {
String s = xrp.getName();
if (s.equals("creature")) {
encounterID = xrp.getAttributeResourceValue(null, "id", 0);
encounterType = xrp.getAttributeValue(null, "type");
encounterName = xrp.getAttributeValue(null, "name");
encounterRace = xrp.getAttributeValue(null, "race");
encounterGender = xrp.getAttributeValue(null, "gender");
encounterAge = xrp.getAttributeResourceValue(null, "age", 0);
encounterAlignment = xrp.getAttributeValue(null, "alignment");
}
}
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// ***** END XML PARSING *****
The following is documentation on how to use the XmlPullParser documentation.
That being said you want to use the xrp.next() function to move the parser to the next element. This will cycle through all the elements in your XML document in the order that they appear. If you have sub elements, the parser will reach those after touching the START_TAG of the parent element.