XmlPullParser gets child nodes

I am working with an OpenStreetMap (.osm) file with Android XmlPullParser. I am having problems with this:

<way id='-13264' action='modify' visible='true'> <nd ref='-13252' /> <nd ref='-13251' /> <nd ref='-13249' /> </way> 

I need to work with nd nodes in every way - node, one way - node at a time (which is cooler), creating a specific data structure between these nodes to be exact. There seems to be no convenient method for getting all the child nodes of a single node in XmlPullParser, so I tried a lot of nested if / elseif-stuff on these nodes, but I can't get it to work. Can someone provide me some sample code for working with child nodes of a node, but are there separate children of similar parent nodes?

+7
source share
2 answers

Here's how I would make it out. You can use it, but you will have to come up with an implementation for the Way class yourself! :)

 List<Way> allWays = new ArrayList<Way>(); Way way; int eventType; while((eventType = parser.getEventType())!=XmlPullParser.END_DOCUMENT){ if(eventType==XmlPullParser.START_TAG) { if("nd".equals(parser.getName()) { way.addNd(parser.getAttributeValue(0)); } else if("way".equals(parser.getName()) { way = new Way(); } } else if(eventType==XmlPullParser.END_TAG) { if("way".equals(parser.getName()) { allWays.add(way); } } parser.next(); } 

Of course, if the xml that suits you is even a little different, this exact code may not work. Again, I will leave this as an exercise for the seeker.

+7
source

You can use the following code:

  int eventType=parser.getEventType(); while(eventType!=XmlPullParser.END_DOCUMENT){ if(eventType==XmlPullParser.START_TAG && parser.getName().equals("nd"){ //process your node... } parser.next(); } 
+2
source

All Articles