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="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

JAVA file

public class MainActivity extends AppCompatActivity {

    TextView tv1;

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

        tv1=(TextView)findViewById(R.id.textView1);

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

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

            Element element=doc.getDocumentElement();
            element.normalize();

            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");
                    tv1.setText(tv1.getText()+"-----------------------");
                }
            }
        } 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();
    }
}

Assest/file.xml

<?xml version="1.0"?>
<records xmlns="">
    <employee>
        <name>Vaibhav</name>
        <surname>Donga</surname>
        <age>20</age>
        <salary>50000</salary>
    </employee>

    <employee>
        <name>Test </name>
        <surname>Varma</surname>
        <age>20</age>
        <salary>60000</salary>
    </employee>

    <employee>
        <name>Dins</name>
        <surname>Hr</surname>
        <age>20</age>
        <salary>70000</salary>
    </employee>

</records>

Please check the below link for XML URL Parser.

https://github.com/dongavaibhav/XmlPrasing

 

Submit a Comment

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

Subscribe

Select Categories