0

I'm currently working with XML parsing but I'm a bit lost on it since first of all I'm more of a fan of JSON than XML. Well anyways I need to got this working so I check out on this link the how to and created a method to do what I need. Basically I just need to pass all of the content of the XML into an ArrayList disregarding the other tags which I won't be using. Here's the code so far:

public ArrayList<HashMap<String, String>> StringToArrayList (String data) throws Exception{
        ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();


        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        HashMap<String, String> hashMap = new HashMap<String, String>();
        String tag = "";

        xpp.setInput(new StringReader (data));
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {


            if(eventType == XmlPullParser.START_DOCUMENT) {
                //Document start
            } else if(eventType == XmlPullParser.END_DOCUMENT) {
                //Document end
            } else if(eventType == XmlPullParser.START_TAG) {
                //Get the tag name then set it into tag String variable
                tag = xpp.getName();
            } else if(eventType == XmlPullParser.END_TAG) {
                if(xpp.getName().equals("start")){
                    //Add the hashMap into the list since the start tag now end
                    arrayList.add(hashMap);
                }
            } else if ((tag.equals("tag1")) && (eventType == XmlPullParser.TEXT)) {
                hashMap.put(tag,xpp.getText());
                tag = "";
            } else if ((tag.equals("tag2")) && (eventType == XmlPullParser.TEXT)) {
                hashMap.put(tag,Uxpp.getText());
                tag = "";
            }

            eventType = xpp.next();
        }

        return arrayList;
    }

and as for a sample my XML looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<content>
  <start>
    <tag1>Tag1 Content 1</tag1>
    <tag2>Tag2 Content 1</tag2>
  </start>
  <start>
    <tag1>Tag1 Content 2</tag1>
    <tag2>Tag2 Content 2</tag2>
  </start>
  <start>
    <tag1>Tag1 Content 3</tag1>
    <tag2>Tag2 Content 3</tag2>
  </start>
</content>

And now instead I got a value of [{tag1=Tag1 Content 1,tag2=Tag2 Content 1},{tag1=Tag1 Content 2,tag2=Tag2 Content2},{tag1=Tag1 Content 3,tag2=Tag2 Content 3}] Instead I got [{tag1=Tag1 Content 3,tag2=Tag2 Content 3},{tag1=Tag1 Content 3,tag2=Tag2 Content 3},{tag1=Tag1 Content 3,tag2=Tag2 Content 3}] And I'm lost on it. Can someone point out how to solve this? And if there is a much simpler way that what I did hope you can help me with it and I would very much appreciate it. :D

1 Answer 1

1

Already got the answer on my problem. It's just that it I need to call a new instance for my HashMap and put it after I add the HashMap into the ArrayList. It will be now:

public ArrayList<HashMap<String, String>> StringToArrayList (String data) throws Exception{
        ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();


        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        HashMap<String, String> hashMap = new HashMap<String, String>();
        String tag = "";

        xpp.setInput(new StringReader (data));
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {


            if(eventType == XmlPullParser.START_DOCUMENT) {
                //Document start
            } else if(eventType == XmlPullParser.END_DOCUMENT) {
                //Document end
            } else if(eventType == XmlPullParser.START_TAG) {
                //Get the tag name then set it into tag String variable
                tag = xpp.getName();
            } else if(eventType == XmlPullParser.END_TAG) {
                if(xpp.getName().equals("start")){
                    //Add the hashMap into the list since the start tag now end
                    arrayList.add(hashMap);
                    //Call a new instance
                    hashMap = new HashMap<String, String>();
                }
            } else if ((tag.equals("tag1")) && (eventType == XmlPullParser.TEXT)) {
                hashMap.put(tag,xpp.getText());
                tag = "";
            } else if ((tag.equals("tag2")) && (eventType == XmlPullParser.TEXT)) {
                hashMap.put(tag,Uxpp.getText());
                tag = "";
            }

            eventType = xpp.next();
        }

        return arrayList;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.