2012-10-17 72 views
0

我正在开发一个应用程序,其中使用其余的webservice从服务器获取xml。从服务器获得响应(以xml格式)后,我使用Dom解析器将其解析为字符串。以下是我的XML响应的示例结构。如何解析嵌套的xml和检索android中的数据?

<appdata> 
    <brand name="Lovely Products"> 
     <product>Hat</product> 
     <rating>Gloves</rating> 
    </brand> 
    <categories> 
     <categorie Table > 
      <sourseUrl Chair> 
       <image available="true" height="400" width="400"> 
       <serverURL http://abcd.xyzpqr.com/images/pi/89 /da/0c=121017011508&=3> 

       </serverURL> 
      </sourseUrl> 
     </categorie> 
     <relatedProducts > 
      <sometag ........> 
        <childtag1.........> </childtag1> 
        <childtag2.........> 
          <tag1.....> 
          <tag2.....> 
        </childtag2> 
      </sometag> 
     </relatedProducts> 

     .......... 
     .......... 
     .......... 
    </categories> 

下面是我的代码通过使HTTP请求

 DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext();  

     HttpHost proxy = new HttpHost("45.28.19.345", 1234); 
     httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 
     HttpGet httpGet = new HttpGet(url); 

     HttpResponse httpResponse = httpClient.execute(httpGet, localContext); 
     HttpEntity httpEntity = httpResponse.getEntity(); 

     xml = getASCIIContentFromEntity(httpEntity);</pre> 

若要通过元件节点名称 公共字符串的getValue(元项目获取每个XML子元素值获取XML内容, String str){Node_N = item.getElementsByTagName(str); return this.getElementValue(n.item(0)); }

public final String getElementValue(Node elem) { 
     Node child; 
     if(elem != null){ 
      if (elem.hasChildNodes()){ 
       for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){ 
        if(child.getNodeType() == Node.TEXT_NODE ){ 
         return child.getNodeValue(); 
        } 
       } 
      } 
     } 
     return ""; 
    } 

这里是如何检索分析的XML数据。

String newxml = xml.toString(); 
    Document doc = parser.getDomElement(newxml); // getting DOM element 

     NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
     // looping through all item nodes <item> 
     for (int i = 0; i < nl.getLength(); i++) { 
      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 
      for(int k = 0; k < doc.getChildNodes().getLength(); k++) { 
       Element e = (Element) nl.item(k); 
       // adding each child node to HashMap key => value 
       map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
       map.put(KEY_NAME, parser.getValue(e, KEY_NAME)); 
        map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST)); 
       map.put(KEY_DESC, parser.getValue(e, KEY_DESC)); 

       // adding HashList to ArrayList 
       menuItems.add(map);</pre> 

getDomElement(字符串XML)方法是

Document doc = null; 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    try { 

     DocumentBuilder db = dbf.newDocumentBuilder(); 

     InputSource is = new InputSource(); 
     if(is != null){ 
      is.setCharacterStream(new StringReader(xml)); 
      doc = db.parse(is); 

通过使用该逻辑是能够检索简单的XML,但不复杂的XML等我如上所述。有人可以给出一个想法,从嵌套的XML获取数据。在Android的

回答