Reputation: 1632
<MyEvents>
<Tracking event="first">
<![CDATA[
http://www.myurl.com/test1
]]>
</Tracking>
<Tracking event="second">
<![CDATA[
http://www.myurl.com/test2
]]>
</Tracking>
<Tracking event="third">
<![CDATA[
http://www.myurl.com/test3
]]>
</Tracking>
<Tracking event="fourth">
<![CDATA[
http://www.myurl.com/test4
]]>
</Tracking>
<Tracking event="fifth">
<![CDATA[
http://www.myurl.com/test5
]]>
</Tracking>
</MyEvents>
I have this xml which i need to parse and store. As each node is named the same "Tracking" I dont know how to parse this xml. I want to parse and store urls corresponding with each event names(first, second, third, fourth and fifth in this case) in variables. I am using XMLPullParser and for other parts of this xml parsing was simple by making use of
if(myparser.getName().equalsIgnoreCase("tagname")){
//do something
}
in case of node with the same name but different event types i dont know how to parse. Any help is appreciated.
Upvotes: 0
Views: 79
Reputation: 1759
First, you may declare a inner class like this:
static class Track {
String event;
String url;
}
Then, the parsing code:
List<Track> allTracks = new ArrayList<Track>();
myparser.require(XmlPullParser.START_TAG, null, "MyEvents");
while (myparser.next() != XmlPullParser.END_TAG) {
if (myparser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = myparser.getName();
if (name.equals("Tracking ")) {
String event = myparser.getAttributeValue(0); // I'm supposing that there is just one
myparser.require(XmlPullParser.START_TAG, null, "Tracking ");
String url = null;
if (myparser.next() == XmlPullParser.TEXT) {
url = parser.getText();
myparser.nextTag();
}
Track track = new Track();
track.event = event;
track.url = url;
allTracks.add(track);
}
}
It's not perfectly but should get a list of tracks, each one with its respective url and event.
Upvotes: 1