Android - XML从URL中提取解析器?

时间:2011-03-23 18:34:02

标签: android xml xmlpullparser

我已经为存储在Assets中的本地XML文件实现了XML Pull Parser。我需要在一些目标URL中为XML实现相同的,如何将URL传递给XML Pull解析器?

Thnks

1 个答案:

答案 0 :(得分:2)

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();

}