如何在android中解析嵌套的xml并检索数据?

时间:2012-10-17 07:22:44

标签: android xml-parsing

我正在开发一个应用程序,其中使用rest webservice从服务器获取xml。从服务器获得响应(以xml格式表示)后,我使用Dom解析器将其解析为字符串。以下是我的xml响应的示例结构。

<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请求获取XML内容的代码

        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>

通过传递元素节点名来获取每个xml子元素值     public String getValue(Element item,String str){         NodeList 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(String 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中获取数据的想法。

1 个答案:

答案 0 :(得分:0)

在android中导入org.xml.sax

有关SAX的详细信息,请参阅http://www.saxproject.org/apidoc/org/xml/sax/package-summary.html