Android Xml Parsing/ SOAP Response Parsing Tutorial Using DOM Parser

0

Android XMl Parser

This tutorial explains, how to parse XML or how to retrieve data From SOAP response. Also Explain, how to update ListView with parsing XML data.



In the previous tutorial, I have explained, how to POST and GET http request for SOAP REST API. Also get XML response from HTTP and convert it into String. In this tutorial, i will get data from XML format string.

XML Structure

We will retrieve data from below XML format

<?xml version="1.0" encoding="UTF-8"?>
     <response>
        <return>
            <user_id>1</user_id>    
            <name>Abhay Anand</name>
            <phone>1234567890</phone>
        </return> 
        <return>
            <user_id>2</user_id>    
            <name>Anand</name>
            <phone>0123456789</phone>
        </return> 
        <return>
            <user_id>3</user_id>    
            <name>Sachin</name>
            <phone>0312456987</phone>
        </return> 
        <return>
            <user_id>4</user_id>    
            <name>Ram</name>
            <phone>0147852369</phone>
        </return> 
        <return>
            <user_id>5</user_id>    
            <name>Shyam</name>
            <phone>3012478596</phone>
        </return> 
     </response>

XML String Format

This is only for demo, If you got XML from Http request then there is no need to make this function use your XML String. This method is used to generate and return string as XML format.

 protected String getXMLString(){
       String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
               "<response>\n" +
               "    <return>\n" +
               "        <user_id>1</user_id>    \n" +
               "        <name>Abhay Anand</name>\n" +
               "        <phone>1234567890</phone>\n" +
               "    </return> \n" +
               "    <return>\n" +
               "        <user_id>2</user_id>    \n" +
               "        <name>Anand</name>\n" +
               "        <phone>0123456789</phone>\n" +
               "    </return> \n" +
               "    <return>\n" +
               "        <user_id>3</user_id>    \n" +
               "        <name>Sachin</name>\n" +
               "        <phone>0312456987</phone>\n" +
               "    </return> \n" +
               "    <return>\n" +
               "        <user_id>4</user_id>    \n" +
               "        <name>Ram</name>\n" +
               "        <phone>0147852369</phone>\n" +
               "    </return> \n" +
               "    <return>\n" +
               "        <user_id>5</user_id>    \n" +
               "        <name>Shyam</name>\n" +
               "        <phone>3012478596</phone>\n" +
               "    </return> \n" +
               "</response>";
        return xmlString;
    }

Parse XMl Content

To get value from XML, we need to create DocumentBuilder object. DocumentBuilder class defines the API to obtain DOM Document instances from an XML document. Also Parse the content of the given InputStream as an XML document and return a new DOM Document object.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String xmlString = getXMLString();

        try {

            DocumentBuilder newDocumentBuilder = 
                DocumentBuilderFactory.newInstance().newDocumentBuilder();

            Document parse = newDocumentBuilder.parse(new 
                ByteArrayInputStream(xmlString.getBytes()));
            

        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

Add ListView in Layout

We will display data using ListView, so need to add ListView tag in Layout file.

<ListView
        android:id="@+id/dataList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ListView>

Android XMl Parser


Use getElementsByTagName()

To get data from XMl tag name, we need to use getElementsByTagName(“Your XMl Tag Name”).

To get length of tag — parse.getElementsByTagName(“Your XMl Tag Name”).getLength().

To get data value — parse.getElementsByTagName(“Your XMl Tag Name”).item(“Index”).getTextContent().

Add data In Listview

We are going to use ListView to display data as list. Create an Adpater object and pass adapter to setAdapter() method of ListView Class.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String xmlString = getXMLString();

        try {

            DocumentBuilder newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document parse = newDocumentBuilder.parse(new ByteArrayInputStream(xmlString.getBytes()));
            List<String> dataList = new ArrayList<String>();

            for (int i=0;i< parse.getElementsByTagName("return").getLength();i++){
                String user_id = parse.getElementsByTagName("user_id").item(i).getTextContent();
                String name = parse.getElementsByTagName("name").item(i).getTextContent();
                String phone = parse.getElementsByTagName("phone").item(i).getTextContent();
                dataList.add(user_id +". " + name +" (" + phone+")");
            }

            final ListView listview = (ListView) findViewById(R.id.dataList);

            ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, dataList);
            listview.setAdapter(adapter);

        } catch(Exception e){
            e.printStackTrace();
        }
    }

    protected String getXMLString(){
       String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
               "<response>\n" +
               "    <return>\n" +
               "        <user_id>1</user_id>    \n" +
               "        <name>Abhay Anand</name>\n" +
               "        <phone>1234567890</phone>\n" +
               "    </return> \n" +
               "    <return>\n" +
               "        <user_id>2</user_id>    \n" +
               "        <name>Anand</name>\n" +
               "        <phone>0123456789</phone>\n" +
               "    </return> \n" +
               "    <return>\n" +
               "        <user_id>3</user_id>    \n" +
               "        <name>Sachin</name>\n" +
               "        <phone>0312456987</phone>\n" +
               "    </return> \n" +
               "    <return>\n" +
               "        <user_id>4</user_id>    \n" +
               "        <name>Ram</name>\n" +
               "        <phone>0147852369</phone>\n" +
               "    </return> \n" +
               "    <return>\n" +
               "        <user_id>5</user_id>    \n" +
               "        <name>Shyam</name>\n" +
               "        <phone>3012478596</phone>\n" +
               "    </return> \n" +
               "</response>";
        return xmlString;
    }
}