How to get data from XMLParsing in android

XML stands for Extensible Mark-up Language.
XML files normally usually parsed in android to retrieve the relevant data from them.
There unit of measurement 3 types of android XML program that we are able to use. There unit of measurement 3 kinds of android XML Parser that we are able to use.

  1. SAX Parsers
  2. DOM Parsers
  3. XML Pull Parser

DOM Parser
DOM parser use Associate in Nursing object based approach wherever the entire XML is loaded into the memory and valid Then it starts parsing the XML document. It parses from the start node to the tip node.

SAX and XML Pull parser
These use Associate in Nursing object primarily based approach and area unit similar in terms of memory and performance.
SAX is appreciate DOM within the context that it begins parsing from prime to bottom and there is no due to break down solely specific nodes.

Associate in Nursing XML file consists of 4 major parts.

Prolog : The first line that contains the information a pair of file is artificial language. generally this is often often the line:

Events : Events in Associate in Nursing XML file embody easy begin and finish tags and extra

Text : It’s easy text in between 2 tags. Example: My Text

Attributes : Attributes unit of measurement the additional properties of a tag that area unit gift inside the tag. As an example, consider some text or nested tags.

Please check below code for XML Pull Parsing

XML file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:layout_height="wrap_content" />

JAVA file

public class MainActivity extends AppCompatActivity {

    TextView tv1;

    protected void onCreate(Bundle savedInstanceState) {


        try {
            InputStream is = getAssets().open("file.xml");

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(is);

            Element element=doc.getDocumentElement();

            NodeList nList = doc.getElementsByTagName("employee");

            for (int i=0; i<nList.getLength(); i++) {

                Node node = nList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element2 = (Element) node;
                    tv1.setText(tv1.getText()+"\nName : " + getValue("name", element2)+"\n");
                    tv1.setText(tv1.getText()+"Surname : " + getValue("surname", element2)+"\n");
                    tv1.setText(tv1.getText()+"Age : " + getValue("age" + "", element2)+"\n");
                    tv1.setText(tv1.getText()+"Salary : " + getValue("salary", element2)+"\n");
        } catch (Exception e) {e.printStackTrace();}

    private static String getValue(String tag, Element element) {
        NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
        Node node = nodeList.item(0);
        return node.getNodeValue();


<?xml version="1.0"?>
<records xmlns="">

        <name>Test </name>



Please check the below link for XML URL Parser.


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories