My code to parse an XML file for my Android game has become too complicated
<p>I finally finished my XML parsing code, and now looking at it, it scares me.  This code is for a simple Android-based, text adventure game. </p>

<p>I have all my data about encounters, locations, and characters stored in XML files in the 'res' folder.  </p>

<p>I coded this XML parser to go through the XML files and store data into a class called Encounter. I can then access the data inside Encounter anywhere in my code.  </p>

<p>So I guess I got the OOP part down.  But the actual parsing just looks so messy.</p>

<p>Is there a better way to go about parsing XML data?</p>

<p>Here is my code:</p>

<pre><code>public class XmlParser extends Activity  {

private String xmlValue;
private int encounterID;
Encounter encounter;

public XmlParser()
    throws XmlPullParserException, IOException
{
    XmlPullParser xpp = getResources().getXml(R.xml.encounters);

    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
     String elName = xpp.getName();
     if(eventType == XmlPullParser.START_TAG) {     //creature
         eventType = xpp.next();

         //top level nodes
         if(xpp.getName().equalsIgnoreCase("identity")){
             eventType = xpp.next();
             if(elName.equalsIgnoreCase("name")) {
                 encounter.name = xpp.getText();
             } else if(elName.equalsIgnoreCase("race")) {
                 encounter.race = xpp.getText();
             } else if(elName.equalsIgnoreCase("gender")) {
                 encounter.gender = xpp.getText();
             } else if(elName.equalsIgnoreCase("alignment")) {
                 encounter.alignment = xpp.getText();
             } else if(elName.equalsIgnoreCase("age")) {
                 try {
                     encounter.age = Integer.parseInt(xpp.getText());
                 } catch (NumberFormatException e) {
                     //ok
                 }
             }
         } else if (xpp.getName().equalsIgnoreCase("appearance")) {
             eventType = xpp.next();
             if(elName.equalsIgnoreCase("condition")){
                 encounter.condition = xpp.getText();
             } else if(elName.equalsIgnoreCase("skinColor")) {
                 encounter.skinColor = xpp.getText();
             } else if(elName.equalsIgnoreCase("hairColor")) {
                 encounter.hairColor = xpp.getText();
             } else if(elName.equalsIgnoreCase("size")) {
                 encounter.size = xpp.getText();
             } else if(elName.equalsIgnoreCase("height")) {
                 encounter.height = xpp.getText();
             } else if(elName.equalsIgnoreCase("weight")) {
                 encounter.weight = xpp.getText();
             }

         } else if (xpp.getName().equalsIgnoreCase("stats")) {
             eventType = xpp.next();
             if(elName.equalsIgnoreCase("hitPoints")) {
                 try {
                     encounter.HP = Integer.parseInt(xpp.getText());
                 } catch (NumberFormatException e) {
                     //ok
                 }
             } else if(elName.equalsIgnoreCase("armorClass")) {
                 try {
                     encounter.AC = Integer.parseInt(xpp.getText());
                 } catch (NumberFormatException e) {
                     //ok
                 }                   
             } else if(elName.equalsIgnoreCase("actionPoints")) {
                 try {
                     encounter.AP = Integer.parseInt(xpp.getText());
                 } catch (NumberFormatException e) {
                     //ok
                 }                   
             } else if(elName.equalsIgnoreCase("magicPoint")) {
                 try {
                     encounter.AP = Integer.parseInt(xpp.getText());
                 } catch (NumberFormatException e) {
                     //ok
                 }                   
             } else if(elName.equalsIgnoreCase("strength")) {
                 try {
                     encounter.strength = Integer.parseInt(xpp.getText());
                 } catch (NumberFormatException e) {
                     //ok
                 }                   
             } else if(elName.equalsIgnoreCase("dexterity")) {
                 try {
                     encounter.dexterity = Integer.parseInt(xpp.getText());
                 } catch (NumberFormatException e) {
                     //ok
                 }
             } else if(elName.equalsIgnoreCase("intelligence")) {
                 try {
                     encounter.intelligence = Integer.parseInt(xpp.getText());
                 } catch (NumberFormatException e) {
                     //ok
                 }
             }

         } else if (xpp.getName().equalsIgnoreCase("inventory")) {
             eventType = xpp.next();
             if(elName.equalsIgnoreCase("weapon")) {
                 encounter.weapon = xpp.getText();
             } else if(elName.equalsIgnoreCase("armor")) {
                 encounter.armor = xpp.getText();
             } else if(elName.equalsIgnoreCase("magicItem")) {
                 encounter.magicItem = xpp.getText();
             }

         } else if (xpp.getName().equalsIgnoreCase("magic")) {
            eventType = xpp.next();
            if(elName.equalsIgnoreCase("attackSpell")) {
                encounter.attackSpell = xpp.getText();
            } else if(elName.equalsIgnoreCase("defenseSpell")) {
                encounter.defenseSpell = xpp.getText();
            }
         } else if (xpp.getName().equalsIgnoreCase("treasureItems")) {
             eventType = xpp.next();
             if(elName.equalsIgnoreCase("item1")) {
                 encounter.item1 = xpp.getText();
             } else if(elName.equalsIgnoreCase("item2")) {
                 encounter.item2 = xpp.getText();
             }
         }
     } else if(eventType == XmlPullParser.END_TAG) {
         //System.out.println("End tag "+xpp.getName());
     }
     eventType = xpp.next();
    }
    //System.out.println("End document");
}
</code></pre>

<p>In case anyone is interested, here is my XML file for encounters:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;encounters&gt;
&lt;creature id="1" type="monster"&gt;
    &lt;identity&gt;
        &lt;name&gt;&lt;/name&gt;
        &lt;race&gt;&lt;/race&gt;
        &lt;gender&gt;&lt;/gender&gt;
        &lt;age&gt;&lt;/age&gt;
        &lt;alignment&gt;&lt;/alignment&gt;
    &lt;/identity&gt;

    &lt;appearance&gt;
        &lt;condition&gt;&lt;/condition&gt;
        &lt;skinColor&gt;&lt;/skinColor&gt;
        &lt;hairColor&gt;&lt;/hairColor&gt;
        &lt;size&gt;&lt;/size&gt;
        &lt;height&gt;&lt;/height&gt;
        &lt;weight&gt;&lt;/weight&gt;
    &lt;/appearance&gt;

    &lt;stats&gt;
        &lt;hitPoints&gt;&lt;/hitPoints&gt;
        &lt;armorClass&gt;&lt;/armorClass&gt;
        &lt;actionPoints&gt;&lt;/actionPoints&gt;
        &lt;magicPoints&gt;&lt;/magicPoints&gt;
        &lt;strength&gt;&lt;/strength&gt;
        &lt;dexterity&gt;&lt;/dexterity&gt;
        &lt;intelligence&gt;&lt;/intelligence&gt;
    &lt;/stats&gt;

    &lt;inventory&gt;
        &lt;weapon&gt;&lt;/weapon&gt;
        &lt;armor&gt;&lt;/armor&gt;
        &lt;magicItem&gt;&lt;/magicItem&gt;
    &lt;/inventory&gt;

    &lt;magic&gt;
        &lt;attackSpell&gt;&lt;/attackSpell&gt;
        &lt;defenseSpell&gt;&lt;/defenseSpell&gt;
    &lt;/magic&gt;

    &lt;treasureItems&gt;
        &lt;item1&gt;&lt;/item1&gt;
        &lt;item2&gt;&lt;/item2&gt;
    &lt;/treasureItems&gt;

&lt;/creature&gt;
</code></pre>

<p></p>
