7

I'm working with an OpenStreetMap (.osm) file with Android's XmlPullParser. The part I'm having problems with is this:

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

I need to work with the nd- nodes within every way- node, one way- node at a time (that's the crux), creating a certain data structure between those nodes to be precise. There seems to be no convenient method to get all the child nodes of one node in the XmlPullParser, so i tried a lot of that nested if/elseif- stuff on those nodes, but can't get it to work. Can someone provide me with some sample code to work with child nodes of a node, but keeping the child nodes of similar parent nodes seperate?

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • came here looking for the exact same thing, wasn't disappointed. Just an FYI, i just open sourced my util for reading osm.bz2 files from jdk/adk https://github.com/spyhunter99/osmreader – spy Apr 30 '16 at 20:07
  • @spy Looks good, thanks for sharing! – fweigl May 01 '16 at 17:31

2 Answers2

8

This is how I would parse this. You are free to use it, but you will have to come up with the implementation for the Way class on your own! :)

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 coming to you is even a slight bit different this exact code may not work. Again, I will leave that as an exercise for the asker.

d3m0li5h3r
  • 1,957
  • 17
  • 33
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • I'll do it a little different but this gave me the right direction. Thanks! – fweigl Sep 19 '11 at 15:58
  • @Ascorbin, I am facing same problem and I am new to the XmlPullParsing please help me. I have posted my question please check this http://stackoverflow.com/questions/17807718/how-to-get-child-or-sub-tags-in-xml-using-xmlpullparsing-in-android – Salman Khan Jul 25 '13 at 05:26
  • I am facing same problem http://stackoverflow.com/questions/39391267/how-can-i-identify-same-xml-tag-second-time – Arpit Patel Sep 09 '16 at 13:58
2

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();
    }
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • Please help me with this http://stackoverflow.com/questions/39391267/how-can-i-identify-same-xml-tag-second-time – Arpit Patel Sep 09 '16 at 14:00