SCEM MOBILE APPLICATION DEVELOPMENT
PART A
Program 6
Create two files of XML and JSON type with values for City_Name, Latitude, Longitude,
Temperature, and Humidity. Develop an application to create an activity with two buttons to
parse the XML and JSON files which when clicked should display the data in their respective
layouts side by side.
Steps:
1. Create a New Android Project with Empty Activity.
2. Open activity_main.xml file from res→ layout folder, check/add ConstraintLayout as the root
view.
3. Create the layout design using Drag and Drop framework.
4. Add Listeners to Button Click Event:
• Create a class which implements OnClickListener interface.
• Override onClick() method of OnClickListener Interface.
• Register the button for click event by calling setOnClickListener() method of View class and
pass the object of the class that implemented OnClickListener Interface.
SCEM MOBILE APPLICATION DEVELOPMENT
5. Create assets folder (Refer below Section).
6. Create input.xml file inside assets folder and paste the below Xml Data.
input.json
Steps:
1. Right click on app ->new->folder->assets folder
2. Right click on assets folder ->new->file->input.json
"employee": {
"city_name": "Mysore",
"Latitude": "12.295",
"Longitude": "76.639",
"Temperature": 22,
"Humidity": "90%"
input.xml
Steps:
1. Right click on app ->new->folder->assets folder
2. Right click on assets folder ->new->file->input.xml
SCEM MOBILE APPLICATION DEVELOPMENT
<?xml version="1.0"?>
<records>
<employee>
<city_name>Mysore</city_name>
<Latitude>12.295</Latitude>
<Longitude>76.639</Longitude>
<Temperature>22</Temperature>
<Humidity>90%</Humidity>
</employee>
<employee>
<city_name>Mysore</city_name>
<Latitude>12.295</Latitude>
<Longitude>76.639</Longitude>
<Temperature>22</Temperature>
<Humidity>90%</Humidity>
</employee>
</records>
SCEM MOBILE APPLICATION DEVELOPMENT
MainActivity.java
package com.example.program6;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnParseXml,btnParseJson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnParseXml=(Button)findViewById(R.id.btn_parsexml);
btnParseJson=(Button)findViewById(R.id.btn_parsejson);
btnParseJson.setOnClickListener(this);
btnParseXml.setOnClickListener(this);
@Override
public void onClick(View v) {
SCEM MOBILE APPLICATION DEVELOPMENT
if(v.equals(btnParseJson))
Intent it=new Intent(this,ViewActivity.class);
it.putExtra("mode",1);
startActivity(it);
else if(v.equals(btnParseXml))
Intent it=new Intent(this,ViewActivity.class);
it.putExtra("mode",2);
startActivity(it);
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
SCEM MOBILE APPLICATION DEVELOPMENT
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_parsexml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Parse XML Data"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<Button
android:id="@+id/btn_parsejson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Parse Json Data"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_parsexml" />
<TextView
SCEM MOBILE APPLICATION DEVELOPMENT
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="PARSING XML AND JSON DATA"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ViewActivity.java
Steps:
1.Under java folder right click on package folder ->new->activity->empty activity->give name
as ViewActivity
package com.example.pogram6;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONObject;
import org.w3c.dom.Document;
SCEM MOBILE APPLICATION DEVELOPMENT
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class ViewActivity extends AppCompatActivity {
TextView lblXmlData,lblJsonData;
int mode=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
lblXmlData=(TextView)findViewById(R.id.lbl_xml_data);
lblJsonData=(TextView)findViewById(R.id.lbl_json_data);
mode=getIntent().getIntExtra("mode",0);
if(mode==1)
parseJson();
else
parseXmlDocument();
}
SCEM MOBILE APPLICATION DEVELOPMENT
public String parseXmlDocument()
try {
InputStream is = getAssets().open("input.xml");
// create a new DocumentBuilderFactory
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
// create a new DocumentBuilder object
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
Element element=doc.getDocumentElement();
//The normalize() method removes empty Text nodes, and joins adjacent Text nodes.
element.normalize();
//List to store multiple employee records
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;
lblXmlData.setText("City Name : " + getValue("city_name", element2)+"\n");
lblXmlData.append("Latitude : " + getValue("Latitude", element2)+"\n");
lblXmlData.append("Longitude : " + getValue("Longitude", element2)+"\n");
SCEM MOBILE APPLICATION DEVELOPMENT
lblXmlData.append("Temperature : " + getValue("Temperature", element2)+"\n");
lblXmlData.append("Humidity : " + getValue("Humidity", element2)+"\n");
catch (Exception e) {e.printStackTrace();}
return null;
private static String getValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = nodeList.item(0);
return node.getNodeValue();
public void parseJson()
try {
InputStream inputStream=getAssets().open("input.json");
//available() method is used to return the number of available bytes left for reading from
the InputStream
byte[] data=new byte[inputStream.available()];
inputStream.read(data);
String readData=new String(data);
SCEM MOBILE APPLICATION DEVELOPMENT
//to read the JSON object
JSONObject jsonObject=new JSONObject(readData);
JSONObject jsonObject1=jsonObject.getJSONObject("employee");
lblJsonData.setText("City Name:"+jsonObject1.getString("city_name")+"\n");
lblJsonData.append("Latitude:"+jsonObject1.getString("Latitude")+"\n");
lblJsonData.append("Longitude"+jsonObject1.getString("Longitude")+"\n");
lblJsonData.append("Temperature:"+jsonObject1.getInt("Temperature")+"\n");
lblJsonData.append("Humidity"+jsonObject1.getString("Humidity")+"\n");
catch (Exception e) {e.printStackTrace();}
activity_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewActivity">
SCEM MOBILE APPLICATION DEVELOPMENT
<TextView
android:id="@+id/lbl_xml_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Xml Data"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="PARSING XML AND JSON DATA"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
SCEM MOBILE APPLICATION DEVELOPMENT
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:text="XML DATA"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="40dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:text="JSON DATA"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/lbl_json_data"
android:layout_width="wrap_content"
SCEM MOBILE APPLICATION DEVELOPMENT
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Json Data"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>
Expected Output