【问题标题】:How to parse the xml from Http Response Android如何从 Http Response Android 解析 xml
【发布时间】:2012-03-07 18:50:03
【问题描述】:

我从 Http 响应中得到了 xml, 谁能给我一个示例,说明如何使用 android 中的 sax 解析器解析该 http 响应?

【问题讨论】:

    标签: android xml httpresponse saxparser


    【解决方案1】:

    本例假设所有数据都在根节点中

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

    【讨论】:

    • 您是否能够解析响应?如果你得到它,请告诉我们。
    【解决方案2】:

    我使用了一个非常小的库(它是两个文件):minimize-xml-parser。这里有一个关于如何使用它的教程: http://www.andengine.org/forums/tutorials/robust-xml-parsing-t4281.html

    图书馆在这里:http://code.google.com/p/minimize-xml-parser/

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-13
        • 1970-01-01
        • 2012-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多