Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6958591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:09:37+00:00 2026-05-27T15:09:37+00:00

I finally finished my XML parsing code, and now looking at it, it scares

  • 0

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.

I have all my data about encounters, locations, and characters stored in XML files in the ‘res’ folder.

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.

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

Is there a better way to go about parsing XML data?

Here is my 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");
}

In case anyone is interested, here is my XML file for encounters:

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

    <appearance>
        <condition></condition>
        <skinColor></skinColor>
        <hairColor></hairColor>
        <size></size>
        <height></height>
        <weight></weight>
    </appearance>

    <stats>
        <hitPoints></hitPoints>
        <armorClass></armorClass>
        <actionPoints></actionPoints>
        <magicPoints></magicPoints>
        <strength></strength>
        <dexterity></dexterity>
        <intelligence></intelligence>
    </stats>

    <inventory>
        <weapon></weapon>
        <armor></armor>
        <magicItem></magicItem>
    </inventory>

    <magic>
        <attackSpell></attackSpell>
        <defenseSpell></defenseSpell>
    </magic>

    <treasureItems>
        <item1></item1>
        <item2></item2>
    </treasureItems>

</creature>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:09:37+00:00Added an answer on May 27, 2026 at 3:09 pm

    You can check out VTD-XML, the lightweight library works
    excellent with android (the larger does too, but it is much larger in filesize).
    There is also the SAX-parser that comes with android.

    Other than that, when I use the XmlPullParser, I usually have a map with the tagnames as keys and integers, then I can just get the value from the map and then use a simple switch, which clears up the code a bit:

    //Before parsing, or if used frequently the map can be put outside the method and reused:
    final HashMap<String, Integer> tags = new HashMap<String, Integer>(9, 1);
    tags.put("encounters", 0);
    tags.put("identity", 1);
    tags.put("name", 2);
    //And so on...
    
    //in your parse-loop:
    int tag = tags.get(xpp.getName());
    switch(tag){
    case 0: //Handle encounter tag...
    break;
    case 1: //Handle identity tag...
    break;
    case 2: //Handle name tag...
    break;
    //For all tags.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm finally getting my team to embrace source code management now that we're working
I've been working on this elevator program for quite some time now and finally
I finally finished coding my Android calculator after so long. But the calculator won't
Still learning a lot.. But basically finally finished my website, but now I'm failing
I've finally finished my killer app and now I've got a bit of an
I finally have finished a Web Application and now I have to make it
I have finally finished my web site. I published it, and I was surprised
I finally have a hardware guy that is insterested in controlling the firmware. This
I finally let windows nag me into upgrading from IE6 to 8. Now in
My index.jsp page, load my form with this line of code : $('#box').load('/Edit_Data.jsp?id=' +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.