2011-03-23 64 views
1

我已經爲存儲在資產中的本地XML文件實現了XML解析器。我需要在某些目標URL中實現相同的XML,如何將URL傳遞給XML Pull解析器?Android - 從URL拉取XML解析器?

Thnks

+0

隨着VTD-XML,它只是調用parseHTTPURL從vtdGen,在兩行。你的應用需要拉動嗎? – 2011-03-24 07:34:20

回答

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

} 
+0

XmlUtils?這個從哪裏來? – mrek 2015-07-24 12:57:16