如何从Http响应解析XML Android

问题描述:

我已经从Http响应的XML, 任何一个可以给我一个例子如何解析在android中使用sax解析器的http响应?如何从Http响应解析XML Android

此示例假设所有的数据都是在根节点

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

您能解析响应吗?请让我们知道,如果你得到它。 – davehale23 2012-03-19 19:45:01

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

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

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