Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse nested xml and retrieve data in android?
    primarykey
    data
    text
    <p>I am developing an application in which am using rest webservice to get xml from server. After getting the response (in terms of xml format) from server, I am parsing it to string by using Dom parser. Following is the sample structure of my xml response. </p> <pre><code>&lt;appdata&gt; &lt;brand name="Lovely Products"&gt; &lt;product&gt;Hat&lt;/product&gt; &lt;rating&gt;Gloves&lt;/rating&gt; &lt;/brand&gt; &lt;categories&gt; &lt;categorie Table &gt; &lt;sourseUrl Chair&gt; &lt;image available="true" height="400" width="400"&gt; &lt;serverURL http://abcd.xyzpqr.com/images/pi/89 /da/0c=121017011508&amp;=3&gt; &lt;/serverURL&gt; &lt;/sourseUrl&gt; &lt;/categorie&gt; &lt;relatedProducts &gt; &lt;sometag ........&gt; &lt;childtag1.........&gt; &lt;/childtag1&gt; &lt;childtag2.........&gt; &lt;tag1.....&gt; &lt;tag2.....&gt; &lt;/childtag2&gt; &lt;/sometag&gt; &lt;/relatedProducts&gt; .......... .......... .......... &lt;/categories&gt; </code></pre> <p></p> <p>following is my code to Getting XML content by making HTTP Request</p> <pre><code> 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);&lt;/pre&gt; </code></pre> <p>To get each xml child element value by passing element node name public String getValue(Element item, String str) { NodeList n = item.getElementsByTagName(str); return this.getElementValue(n.item(0)); }</p> <pre><code>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 ""; } </code></pre> <p>Here is the way how am retrieving the parsed xml data.</p> <pre><code> String newxml = xml.toString(); Document doc = parser.getDomElement(newxml); // getting DOM element NodeList nl = doc.getElementsByTagName(KEY_ITEM); // looping through all item nodes &lt;item&gt; for (int i = 0; i &lt; nl.getLength(); i++) { // creating new HashMap HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); for(int k = 0; k &lt; doc.getChildNodes().getLength(); k++) { Element e = (Element) nl.item(k); // adding each child node to HashMap key =&gt; 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);&lt;/pre&gt; </code></pre> <p>getDomElement(String xml) method is</p> <pre><code> 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); </code></pre> <p>By using this logic am able to retrieve simple xml, but not complex xml like i mentioned above. Can some one give an idea to get the data from that nested xml.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload