我该如何解析xml?

时间:2016-07-21 03:30:19

标签: android xml-parsing

在我的项目中,url返回xml数据,如何解析xml? enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用XmlPullParser按标记解析数据。

Example

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
     factory.setNamespaceAware(true);
     XmlPullParser xpp = factory.newPullParser();

     xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
     int eventType = xpp.getEventType();
     while (eventType != XmlPullParser.END_DOCUMENT) {
      if(eventType == XmlPullParser.START_DOCUMENT) {
          System.out.println("Start document");
      } else if(eventType == XmlPullParser.END_DOCUMENT) {
          System.out.println("End document");
      } else if(eventType == XmlPullParser.START_TAG) {
          System.out.println("Start tag "+xpp.getName());
      } else if(eventType == XmlPullParser.END_TAG) {
          System.out.println("End tag "+xpp.getName());
      } else if(eventType == XmlPullParser.TEXT) {
          System.out.println("Text "+xpp.getText());
      }
      eventType = xpp.next();
     }