Android - XML Pull parser from URL?

2019-06-04 05:28发布

I have implemented XML Pull Parser for local XML file stored in Assets. I need to implement the same for XML in some destinated URL, how to pass URL to XML Pull parser?

Thnks

1条回答
等我变得足够好
2楼-- · 2019-06-04 06:22
public static void getAllXML(String url) throws 

XmlPullParserException, IOException, URISyntaxException{ 

  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

  factory.setNamespaceAware(true);

  XmlPullParser parser = factory.newPullParser(); 

  parser.setInput(new InputStreamReader(getUrlData(url)));  

  XmlUtils.beginDocument(parser,"results");

  int eventType = parser.getEventType();

  do{

    XmlUtils.nextElement(parser);

    parser.next();

    eventType = parser.getEventType();

    if(eventType == XmlPullParser.TEXT){

      Log.d("test",parser.getText());

    }

  } while (eventType != XmlPullParser.END_DOCUMENT) ;       

}

public InputStream getUrlData(String url) 

throws URISyntaxException, ClientProtocolException, IOException {

  DefaultHttpClient client = new DefaultHttpClient();

  HttpGet method = new HttpGet(new URI(url));

  HttpResponse res = client.execute(method);

  return  res.getEntity().getContent();

}
查看更多
登录 后发表回答