简体   繁体   中英

How to parse the xml from Http Response Android

我有来自Http Response的xml,有没有人能给我一个如何使用android中的sax解析器解析那个http响应的例子?

This example assumes that all data is in the root node

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();           
try {
    DocumentBuilder builder = builderFactory.newDocumentBuilder();

    try {
        URL url;
        url = new URL("http://yourURLhere.com" );

        Document document = builder.parse(new InputSource( url.openStream()));

        Element rootElement = document.getDocumentElement();

        try{
            //get the different xml fields here
            Date xmlDateLastNotification = new Date(rootElement.getAttribute("dateLastNotification").toString());
            String someOtherVariable = rootElement.getAttribute("xmlNode").toString();

        }catch(Exception e){}


    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

I've used a very little library (it is two files): minimize-xml-parser. There is a tutorial on how to use it here: http://www.andengine.org/forums/tutorials/robust-xml-parsing-t4281.html

And the library is here: http://code.google.com/p/minimize-xml-parser/

用于在Android 链接中解析和使用XML数据和Web服务的简化源代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM