开发者

How to retrieve external XML files into android application

In my Android application, I want to retrieve an XML file from the internet and parse some tags in that file to my application. I got a working tutorial here which uses XMLResourceParser but that tutorial points out how to re开发者_开发知识库ad an XML file from a local directory.

What are the method calls to retrieve an external XML file into my Android application?

XmlResourceParser xrp = this.getResources().getXml(R.xml.test);


You can get an InputStream to the XML file using the following code:

URL url = new URL(urlString);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)connection;
httpConn.setDoInput(true);
httpConn.setRequestProperty("charset", "utf-8");
int responseCode = httpConn.getResponseCode();
if(responseCode != HttpStatus.SC_OK) {
    InputStream xmlStream = httpConn.getInputStream();

    // Pass the xmlStream object to a XPP, SAX or DOM parser.
}


create a new folder "raw" in "res", put external xml in raw folder

 InputStream inputStream = getResources().openRawResource(R.raw.yourXMLname);


If by "external XML file" you mean loading an XML file from the internet, check out this article on HelloAndroid for how to download an URL to your device.

In terms of the actual XML parsing, I've found SJXP "Simple Java XML Parser" to be the simplest solution. It's lightweight, easy to use, and works using callback handlers. The website also has some sample code which is very helpful. http://www.thebuzzmedia.com/software/simple-java-xml-parser-sjxp/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜