1

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

谢谢

4

1 回答 1

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

}
于 2011-10-03T07:14:11.113 回答