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