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

  • Home
  • SEARCH
  • 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 6674353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:44:07+00:00 2026-05-26T03:44:07+00:00

I am getting the most bizzarre behavior with trying to parse an XML ,

  • 0

I am getting the most bizzarre behavior with trying to parse an XML, I run through it step by step and all values are assigned and retrieved in order and then the object I create is added to a HashMap for easy look up, the problem is when I am done retrieving it all the HashMap has null values and the ones that aren’t null are the value of the very last node that was read, I have walked through it over and over and it all seems correct, but when it’s done loading the values in the HasMap look like:

[0] null
[1] NarrationItem@44e9d170
[2] null
[3] null
[4] NarrationItem@44e9d170

etc, etc.

The format of my XML files is:

<narrations>
    <narration id="0" name="A" alias="a" >
        <line text="This is a test."></line>
    </narration>
    <narration id="1" name="B" alias="b" >
        <line text="This another a test."></line>
    </narration>
    <narration id="2" name="C" alias="c" >
        <line text="This is STILL a test."></line>
    </narration>
</narrations>

And my XML parsing method is follows:

 public HashMap<String, NarrationItem> NarrationMap = new HashMap<String, NarrationItem>();

 private void LoadNarrationsXML() {
     NarrationItem i = new NarrationItem();
     String line;
     String s;

     try {
         // Get the Android-specific compiled XML parser.
         XmlResourceParser xmlParser = this.getResources().getXml(R.xml.narrations);
         while (xmlParser.getEventType() != XmlResourceParser.END_DOCUMENT) {
             if (xmlParser.getEventType() == XmlResourceParser.START_TAG) {
                  s = xmlParser.getName();
                  if (s.equals("narration")) {
                      i.Clear();
                      i.ID  = xmlParser.getAttributeIntValue(null, "id", 0);
                      i.Name = xmlParser.getAttributeValue(null, "name");
                      i.Alias = xmlParser.getAttributeValue(null, "alias");
                  } else if (s.equals("line")) { 
                      line = xmlParser.getAttributeValue(null, "text");
                      i.Narration.add(line);
                  }
            } else if (xmlParser.getEventType() == XmlResourceParser.END_TAG) {
                  s = xmlParser.getName();
                  if (s.equals("narration")) {
                       NarrationMap.put(i.Alias, i);
                  }

             } 
             xmlParser.next();
         }
         xmlParser.close();
     } catch (XmlPullParserException xppe) {
          Log.e(TAG, "Failure of .getEventType or .next, probably bad file format");
          xppe.toString();
     } catch (IOException ioe) {
          Log.e(TAG, "Unable to read resource file");
          ioe.printStackTrace();
     }
 }

The NarrationItem object is a custom object defined as:

public class NarrationItem {

int ID;
String Name;
String Alias;
ArrayList<String> Narration = new ArrayList<String>(); 


public NarrationItem() { }

public void LoadNarration(int id, String name, String alias, ArrayList<String> narration) {

    ID = id;
    Name = name;
    Alias = alias;
    Narration.addAll(narration);// = narration;
}


public void Clear() {
    ID = 0;
    Name = "";
    Alias = "";
    Narration.clear();
}

  }//End Narration

If someone could point out the problem I’d be very thankful I have sat here staring at this issue for hours.

  • 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-26T03:44:07+00:00Added an answer on May 26, 2026 at 3:44 am

    You’re only ever creating one NarrationItem object – you’re then using a reference to that object as the value for multiple entries in the map. Don’t do that. You need to understand that the map doesn’t contain an object as the value – it contains a reference to an object.

    You can probably fix this just by creating a new NarrationItem each time instead of calling Clear.

    It’s not clear how you’re looking at the map to see those null values, but if you’re using the debugger and looking at the internal data structure, you probably shouldn’t really be doing that either – instead, step through the keys, values or entries, i.e. stick within the abstraction that HashMap is meant to support.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the fastest / most efficient way of getting all the distinct items
Most of the code works as expected, but I'm getting some unusual behavior under
I'm trying to parse a string into an XElement using .net. I'm getting this
For most (all?) Facebook pages, I can access its graph data by getting the
What are the most common reasons that I would be getting this error from
Why am I getting no attribute __getitem__ error for dictionary: Traceback (most recent call
Where do you have the most success getting your ArcGIS development questions answered? ESRI
I am getting the following error: Traceback (most recent call last): File ../tests.py, line
What is the most efficient way of getting current time/date/day/year in C language? As
I need some help in getting the name of the most recent directory in

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.