XML : How to get nested tags with XmlPullParser

Help me. I want parse Yahoo Geoplanet. Example XML code

  <place yahoo:uri="http://where.yahooapis.com/v1/place/44418" xml:lang="en-US">    <woeid>44418</woeid>    <name>London</name>    <country type="Country" code="GB" woeid="23424975">United Kingdom</country>    <centroid>      <latitude>51.507702</latitude>      <longitude>-0.127970</longitude>    </centroid>    <boundingBox>      <southWest>        <latitude>51.286839</latitude>        <latitude>51.286839</latitude>      </southWest>      <northEast>        <latitude>51.692322</latitude>        <longitude>0.334030</longitude>      </northEast>    </boundingBox>  </place>    

I want get name, country. Also need get latitude and longitude only in centroid. But gets lat and long in boundBox

          XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();          pullParser.setInput(new InputStreamReader(inputStream));          int event = pullParser.getEventType();            CitySearch citySearch = null;          String tagName;          String currentTag = null;            while (event != XmlPullParser.END_DOCUMENT) {              tagName = pullParser.getName();                if (event == XmlPullParser.START_TAG) {                  if (tagName.equals("place")) {                      citySearch = new CitySearch();                  }                  currentTag = tagName;              } else if (event == XmlPullParser.TEXT) {                  if ("name".equals(currentTag)) {                      assert citySearch != null;                      citySearch.setCityName(pullParser.getText());                  } else if ("country".equals(currentTag)) {                      assert citySearch != null;                      citySearch.setCountry(pullParser.getText());                  } else if ("latitude".equals(currentTag)) {                      assert citySearch != null;                      citySearch.setLatitude(pullParser.getText());                  } else if ("longitude".equals(currentTag)) {                      assert citySearch != null;                      citySearch.setLongitude(pullParser.getText());                  }              } else if (event == XmlPullParser.END_TAG) {                  if ("place".equals(tagName)) {                      resultSearch.add(citySearch);                  }              }                event = pullParser.next();          }    

This code parse name, country and all latitude and longitude. How to get the latitude and longitude only in the centroid, also need name and country

No comments:

Post a Comment