Thursday 13 June 2013

How to read a xml file and display in listview in Android

Documentbuilder defines the API to obtain DOM Document instances from an XML document. Using this class, an application programmer can obtain a Document from XML.
An instance of this class can be obtained from the newDocumentBuilder() method. Once an instance of this class is obtained, XML can be parsed from a variety of input sources. These input sources are InputStreams, Files, URLs, and SAX InputSources.
Note that this class reuses several classes from the SAX API. This does not require that the implementor of the underlying DOM implementation use a SAX parser to parse XML document into a Document. It merely requires that the implementation communicate with the application using these existing APIs.

Here is the employeedetails.xml

<?xml version="1.0" encoding="utf-8" ?>
<Employee>
  <Details>
    <FirstName>Allen</FirstName>
    <LastName>Mclleum</LastName>
    <Age>32</Age>
    <Designation>Software Enginner</Designation>
    <State>Washigton</State>  
  </Details>
  <Details>
    <FirstName>Joe</FirstName>
    <LastName>Josh</LastName>
    <Age>19</Age>
    <Designation>Junior Programmer</Designation>
    <State>Kerala</State>
  </Details>
  <Details>
    <FirstName>Vikas</FirstName>
    <LastName>Rai</LastName>
    <Age>27</Age>
    <Designation>Designer</Designation>
    <State>Delhi</State>
  </Details>
  <Details>
    <FirstName>Silpa</FirstName>
    <LastName>Sharma</LastName>
    <Age>27</Age>
    <Designation>HR</Designation>
    <State>Mumbai</State>
  </Details>
</Employee>

In activity

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * Created by seguro6 on 6/12/13.
 */
public class ReadXml extends ListActivity {

    ArrayList<String> xmlList = new ArrayList<String>();
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try
        {
            InputStream is=getResources().openRawResource(R.raw.employeedetails);
            DocumentBuilder builder= DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc=builder.parse(is, null);
            NodeList nodes=doc.getElementsByTagName("Details");
            if(nodes != null && nodes.getLength() > 0) {
                xmlList.clear();
                int len = nodes.getLength();
                for(int i = 0; i < len; ++i) {
                    // query value
                    Node node = nodes.item(i);
                    xmlList.add(node.getTextContent());
                }
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xmlList);
            setListAdapter(adapter);
        }
        catch(Throwable t){
            Toast.makeText(this, "Exception :" + t.toString(), Toast.LENGTH_LONG).show();
        }
    }
}

1 comment: