XML Parsing

MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
 public class MainActivity extends AppCompatActivity {
  ArrayList < HashMap < String, String >> contactList;
  Button btnParseJson;
  @Override 
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   contactList = new ArrayList < > ();
   btnParseJson = (Button) findViewById(R.id.idBtnParseXML);
   btnParseJson.setOnClickListener(new View.OnClickListener() {
    @Override 
     public void onClick(View view) {
     beginXMLParsing();
   }
  });
 }
  private void beginXMLParsing() {
   InputStream is = null;
  try {
    is = getAssets().open("parsingData/data.xml");
  } catch (IOException e) {
    e.printStackTrace();
   
  }
  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
   DocumentBuilder dBuilder = null;
  
  try {
    dBuilder = dbFactory.newDocumentBuilder();
     } catch (ParserConfigurationException e) {
    e.printStackTrace();
   
  }
   Document doc = null;
  
  try {
    doc = dBuilder.parse(is);
   
  } catch (SAXException e) {
    e.printStackTrace();
   
  } catch (IOException e) {
    e.printStackTrace();
   
  }
   Element element = doc.getDocumentElement();
   element.normalize();
   NodeList nList = doc.getElementsByTagName("rootnode");
  
  for (int i = 0; i < nList.getLength(); i++) {
    Node node = nList.item(i);
   
   if (node.getNodeType() == Node.ELEMENT_NODE) {
     Element element2 = (Element) node;
     String id = element2.getElementsByTagName("Id").item(0).getTextContent();
     String name = element2.getElementsByTagName("Name").item(0).getTextContent();
     String surname = element2.getElementsByTagName("Surname").item(0).getTextContent();
     String age = element2.getElementsByTagName("Age").item(0).getTextContent();
     addingValuesToHasMap(id, name, surname, age);
     Toast.makeText(this, "Data: " + element2.getElementsByTagName("Id").item(0).getTextContent(), Toast.LENGTH_SHORT).show();
    
   }
   
  }
   ListView lv = (ListView) findViewById(R.id.idLvJson);
   ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList, 87 R.layout.list_item, new String[] {
   "Id",
   "Name",
   "Surname",
   "Age"
  }, 
   new int[] {
   R.id.idSNO, R.id.idName, R.id.idSurname, R.id.idAge
  });
   lv.setAdapter(adapter);
  
 }
 private void addingValuesToHasMap(String id, String name, String surname, String age) {
   // tmp hash map for single contact 
   HashMap < String, String > contact = new HashMap < > ();
   // adding each child node to HashMap key => value 
   contact.put("Id", id);
   contact.put("Name", name);
   contact.put("Surname", surname);
   contact.put("Age", age);
   // adding contact to contact list 
   contactList.add(contact);
 }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent"
     android:layout_height="match_parent" 
     tools:context="html.tutorial.com.parsing.MainActivity"
     android:orientation="vertical">  
    
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/idBtnParseXML"
     android:text="Parse XML"  /> 
    
    <ListView 
     android:layout_width="match_parent"
     android:layout_height="match_parent" 
     android:id="@+id/idLvJson"></ListView> 
</LinearLayout> 

list_item.xml
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent"
     android:padding="16dp" 
     android:layout_height="match_parent">  
    
    <TextView 
     android:id="@+id/idSNO" 
     android:layout_width="32dp" 
     android:layout_height="wrap_content" 
     android:text="1" 
     android:textColor="@color/colorAccent" />
    
    <TextView 
     android:id="@+id/idName"
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"
     android:textColor="#5d5d5d" 
     android:text="Arjun" 
     android:textStyle="bold" /> 
    
    <TextView 
     android:id="@+id/idSurname" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#5d5d5d" 
     android:text="Toshniwal" 
     android:textStyle="bold" />  
    
    <TextView 
     android:id="@+id/idAge" 
     android:layout_width="24dp" 
     android:layout_height="wrap_content" 
     android:textColor="#5d5d5d" 
     android:text="28" 
     android:textStyle="bold" />  
</LinearLayout> 

data.xml
<?xml version="1.0" encoding="UTF-8" ?> 
<root> 
    <rootnode> 
        <Id>1</Id> 
        <Name>Arjun</Name>  
        <Surname>Toshniwal</Surname> 
        <Age>25</Age> 
    </rootnode>  
    <rootnode> 
        <Id>2</Id> 
        <Name>Anuradha</Name> 
        <Surname>Awale</Surname> 
        <Age>24</Age> 
    </rootnode> 
    <rootnode> 
        <Id>3</Id>  
        <Name>Ankit</Name>  
        <Surname>Toshniwal</Surname>  
        <Age>21</Age> 
    </rootnode> 
    <rootnode> 
        <Id>4</Id>  
        <Name>Ganesh</Name>  
        <Surname>Mudkhede</Surname>  
        <Age>26</Age> 
    </rootnode> 
</root> 

Share this