read complex XML file android
<p>i have been trying to find a solution to  read a nested xml file like this</p>

<pre><code> &lt;wireSize name="10-AWG"&gt;
&lt;wirematerial name="Cooper"&gt;
  &lt;conduitmaterial name="PVC"&gt;
    &lt;wireresitance name="1.2"&gt;&lt;/wireresitance&gt;
  &lt;/conduitmaterial&gt;
  &lt;conduitmaterial name="Aluminum"&gt;
    &lt;wireresitance name="1.2"&gt;&lt;/wireresitance&gt;
  &lt;/conduitmaterial&gt;
  &lt;conduitmaterial name="Steel"&gt;
    &lt;wireresitance name="1.2"&gt;&lt;/wireresitance&gt;
  &lt;/conduitmaterial&gt;
&lt;/wirematerial&gt;
&lt;wirematerial name="Aluminum"&gt;
  &lt;conduitmaterial name="PVC"&gt;
    &lt;wireresitance name="2.0"&gt;&lt;/wireresitance&gt;
  &lt;/conduitmaterial&gt;
  &lt;conduitmaterial name="Aluminum"&gt;
    &lt;wireresitance name="2.0"&gt;&lt;/wireresitance&gt;
  &lt;/conduitmaterial&gt;
  &lt;conduitmaterial name="Steel"&gt;
    &lt;wireresitance name="2.0"&gt;&lt;/wireresitance&gt;
  &lt;/conduitmaterial&gt;
&lt;/wirematerial&gt;
</code></pre>

<p></p>

<p>so far i was able to load the xml file using this code </p>

<pre><code>InputStream in = getResources().openRawResource(R.raw.table_8);    



            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
            DocumentBuilder db = null;
            try {
                db = dbf.newDocumentBuilder ();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Document document = null;
            try {
                document = db.parse(in, null);
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
</code></pre>

<p>but after that I am clueless what to do</p>

<p>ok after reading and surfing the web i get to this easy solution using the XPath.</p>

<p>reformat the xml file as bellow</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;table8&gt;
&lt;wireSize size="12-AWG"&gt;
    &lt;wirematerial wmaterial ="Cooper"&gt;
                &lt;conduitmaterial cmaterial ="PVC"&gt;
                               &lt;wireresitance&gt;2.2&lt;/wireresitance&gt;
                &lt;/conduitmaterial&gt;
                &lt;conduitmaterial cmaterial ="Aluminum"&gt;
                               &lt;wireresitance&gt;2.6&lt;/wireresitance&gt;
                &lt;/conduitmaterial&gt;
                &lt;conduitmaterial cmaterial ="Steel"&gt;
                               &lt;wireresitance&gt;2.9&lt;/wireresitance&gt;
                &lt;/conduitmaterial&gt;
    &lt;/wirematerial&gt;
    &lt;wirematerial wmaterial ="Aluminum"&gt;
                &lt;conduitmaterial cmaterial ="PVC"&gt;
                               &lt;wireresitance&gt;3.2&lt;/wireresitance&gt;
                &lt;/conduitmaterial&gt;
                &lt;conduitmaterial cmaterial ="Aluminum"&gt;
                               &lt;wireresitance&gt;3.6&lt;/wireresitance&gt;
                &lt;/conduitmaterial&gt;
                &lt;conduitmaterial cmaterial ="Steel"&gt;
                               &lt;wireresitance&gt;3.9&lt;/wireresitance&gt;
                &lt;/conduitmaterial&gt;
    &lt;/wirematerial&gt;
&lt;/wireSize&gt;
&lt;/table8&gt;
</code></pre>

<p>and the android java code </p>

<pre><code>import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class Test6Activity extends Activity {
ArrayList&lt;String&gt; mPeople = new ArrayList&lt;String&gt;();
private ListView m_listview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    m_listview = (ListView) findViewById(R.id.listView1);
    try {
        parseData();
    } catch(Exception ex) {
        Toast.makeText(this, "Exception: " + ex.getMessage(),                             Toast.LENGTH_LONG).show();
    }

    // pass adapter w/ data queried through XPath to ListView
    ArrayAdapter&lt;String&gt; adapter = 
            new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, mPeople);
    m_listview.setAdapter(adapter);
}
private void parseData() throws Exception {
    // create an InputSource object from /res/raw
    InputSource inputSrc = new InputSource(getResources().openRawResource(R.raw.data));
    // query XPath instance, this is the parser
    XPath xpath = XPathFactory.newInstance().newXPath();
    // specify the xpath expression
    String expression = "//wireSize[@size='12-AWG']/wirematerial[@wmaterial='Cooper']/conduitmaterial[@cmaterial='Aluminum']";
    // list of nodes queried
    NodeList nodes = (NodeList)xpath.evaluate(expression, inputSrc, XPathConstants.NODESET);


    Toast.makeText(this, "count: " + String.valueOf(nodes.getLength()),Toast.LENGTH_SHORT).show();
    // if node found
    if(nodes != null &amp;&amp; nodes.getLength() &gt; 0) {
        mPeople.clear();
        int len = nodes.getLength();
        for(int i = 0; i &lt; len; ++i) {
            // query value
            Node node = nodes.item(i);
            mPeople.add(node.getTextContent());
        }
    }
}
</code></pre>

<p>}</p>

<p>after you run the project it give you the value of 2.6
hope this help somebody</p>

<p>any help  would be appreciated 
best regards</p>
