Android Xml Parsing/ SOAP Response Parsing Tutorial Using DOM 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.

Don't Miss

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>

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;
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here

564FansLike

Recent Posts

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.

Related Articles

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.
WP2Social Auto Publish Powered By : XYZScripts.com