0% found this document useful (0 votes)
40 views5 pages

XML and JSON Data Parsing in Android

This document describes a program that parses XML and JSON data. It includes XML and Java code to define a layout with buttons to parse XML and JSON data, and functions to parse and display weather data from an XML file and JSON file including city name, latitude, longitude, temperature, and humidity. When the buttons are clicked, it sets the text of text views to display the weather details parsed from the respective XML and JSON files stored in the assets folder.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views5 pages

XML and JSON Data Parsing in Android

This document describes a program that parses XML and JSON data. It includes XML and Java code to define a layout with buttons to parse XML and JSON data, and functions to parse and display weather data from an XML file and JSON file including city name, latitude, longitude, temperature, and humidity. When the buttons are clicked, it sets the text of text views to display the weather details parsed from the respective XML and JSON files stored in the assets folder.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 Program 6: Parsing XML & JSON Data

XML Code:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<[Link]
android:id="@+id/parse_xml"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_marginBottom="8dp"
android:text="Parse XML Data"
app:layout_constraintBottom_toTopOf="@+id/parse_json"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.24000001"
app:layout_constraintVertical_chainStyle="packed" />

<[Link]
android:id="@+id/parse_json"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_marginBottom="8dp"
android:text="Parse JSON Data"
app:layout_constraintBottom_toTopOf="@+id/data_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/parse_xml" />

<[Link]
android:id="@+id/data_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="16dp"
android:text=""
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/city_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/parse_json" />

<[Link]
android:id="@+id/city_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="City Name :"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/latitude"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
Mobile Application Development – 18CSMP68
2 Program 6: Parsing XML & JSON Data

app:layout_constraintTop_toBottomOf="@+id/data_type" />

<[Link]
android:id="@+id/latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Latitude :"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/longitude"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/city_name" />

<[Link]
android:id="@+id/longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Longitude :"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/temprature"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/latitude" />

<[Link]
android:id="@+id/temprature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Temprature :"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/humidity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/longitude" />

<[Link]
android:id="@+id/humidity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Humidity :"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/temprature" />

</[Link]>

Mobile Application Development – 18CSMP68


3 Program 6: Parsing XML & JSON Data

Java Code:
package `in`.[Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


private lateinit var parseXMLBtn: Button
private lateinit var parseJSONBtn: Button

private lateinit var datatype : TextView


private lateinit var cityName: TextView
private lateinit var latitude: TextView
private lateinit var longitude: TextView
private lateinit var temprature: TextView
private lateinit var humidity: TextView

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_main)

parseXMLBtn = findViewById([Link].parse_xml)
[Link] { parseXML() }
parseJSONBtn = findViewById([Link].parse_json)
[Link] { parseJSON() }
[Link] { parseXML() }

datatype = findViewById([Link].data_type)
cityName = findViewById([Link].city_name)
latitude = findViewById([Link])
longitude = findViewById([Link])
temprature = findViewById([Link])
humidity = findViewById([Link])

@SuppressLint("SetTextI18n")
fun parseXML(){
[Link] = "XML Data"
try {
val iStream = [Link]("[Link]")
val builderFactory = [Link]()
var docBuilder = [Link]()
var doc = [Link](iStream)
[Link] = "City Name : " +
[Link]("City_Name").item(0).getFirstChild().getNodeValue(
)
[Link] = "Latitude : " +
[Link]("Latitude").item(0).getFirstChild().getNodeValue()
[Link] = "Longitude : " +
[Link]("Longitude").item(0).getFirstChild().getNodeValue(
)
[Link] = "Temperature : " +
[Link]("Temperature").item(0).getFirstChild().getNodeValu
Mobile Application Development – 18CSMP68
4 Program 6: Parsing XML & JSON Data

e()
[Link] = "Humidity : " +
[Link]("Humidity").item(0).getFirstChild().getNodeValue()
}
catch (ex: IOException) {

}
}

@SuppressLint("SetTextI18n")
fun parseJSON(){
[Link] = "JSON Data"
val obj = JSONObject(loadJSONFromAsset())
[Link] = "City Name : " + [Link]("City Name")
[Link] = "Latitude : " + [Link]("Latitude")
[Link] = "Longitude : " + [Link]("Longitude")
[Link] = "Temperature : " + [Link]("Temperature")
[Link] = "Humidity : " + [Link]("Humidity")
}

private fun loadJSONFromAsset(): String {


val json: String?
try {
val inputStream = [Link]("[Link]")
val size = [Link]()
val buffer = ByteArray(size)
val charset: Charset = Charsets.UTF_8
[Link](buffer)
[Link]()
json = String(buffer, charset)
}
catch (ex: IOException) {
[Link]()
return ""
}
return json
}
}

Output:

Mobile Application Development – 18CSMP68


5 Program 6: Parsing XML & JSON Data

Mobile Application Development – 18CSMP68

You might also like