Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JSON-PARSER
- 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.
- 1) Firstly, Create an Application by Name “JsonParser”
- 2) Go to xml code of design change the layout to “RelativeLayout”
- 3) Add TextView component & change the following properties:
- 1) Size: 38dp
- 2) Text: XML and JSON Parser
- 3) Center-Align
- 4) Add Two Buttons to Design & change the name “ParseXml” & “ParseJson” with following
- onclick functions:
- • ParseXml-Button: parsexml
- • ParseJson-Button: parsejson
- 5) Add TextView component & change the following properties:
- • Id: display
- • Text: “”
- • Align: Center
- 6) Add Assets folder by following the given hierarchy:
- App->new->folder->Assests folder
- 7) Inside the assets folder create new files of xml and json using the following hierarchy:
- new->file->city.xml
- new->file->city.json
- once created place the following details inside the “city.xml” and “city.json”
- city.xml:
- <?xml version="1.0"?>
- <records>
- <place>
- <name>Mysore</name>
- <lat>12.295</lat>
- <long>76.639</long>
- <temperature>22</temperature>
- <humidity>90%</humidity>
- </place>
- <place>
- <name>Bangalore</name>
- <lat>13.295</lat>
- <long>77.639</long>
- <temperature>25</temperature>
- <humidity>74%</humidity>
- </place>
- </records>
- city.json:
- [
- {
- "name":"HASSAN",
- "lat":"12.295",
- "long":"76.639",
- "temperature":"22",
- "humidity":"92%"
- },
- {
- "name":"MANDYA",
- "lat":"10.11",
- "long":"66.639",
- "temperature":"24",
- "humidity":"82%"
- }
- ]
- XML-CODE:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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=".MainActivity">
- <TextView
- android:id="@+id/textView"
- android:layout_width="257dp"
- android:layout_height="59dp"
- android:layout_alignParentEnd="true"
- android:layout_alignParentBottom="true"
- android:layout_marginEnd="74dp"
- android:layout_marginBottom="453dp"
- android:text="PARSER"
- android:textSize="36sp"
- tools:layout_editor_absoluteX="194dp"
- tools:layout_editor_absoluteY="126dp" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentEnd="true"
- android:layout_alignParentBottom="true"
- android:layout_marginEnd="260dp"
- android:layout_marginBottom="371dp"
- android:backgroundTint="#F1B763"
- android:onClick="parsexml"
- android:text="XML"
- android:textAlignment="center" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentEnd="true"
- android:layout_alignParentBottom="true"
- android:layout_marginEnd="118dp"
- android:layout_marginBottom="373dp"
- android:backgroundTint="#CDDC39"
- android:onClick="parsejson"
- android:text="JSON"
- android:textAlignment="center" />
- <TextView
- android:id="@+id/display"
- android:layout_width="402dp"
- android:layout_height="332dp"
- android:layout_alignParentEnd="true"
- android:layout_alignParentBottom="true"
- android:layout_marginEnd="9dp"
- android:layout_marginBottom="11dp"
- android:textAlignment="center"
- android:textColor="#EF3A78" />
- </RelativeLayout>
- JAVA-CODE
- package com.example.parserapplication;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import java.io.InputStream;
- import java.nio.charset.StandardCharsets;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- public class MainActivity extends AppCompatActivity {
- TextView display;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- display=findViewById(R.id.display);
- }
- public void parsexml(View v){
- try {
- InputStream is=getAssets().open("city.xml");
- DocumentBuilderFactory documentBuilderFactory =
- DocumentBuilderFactory.newInstance();
- DocumentBuilder
- documentBuilder=documentBuilderFactory.newDocumentBuilder();
- Document document=documentBuilder.parse(is);
- StringBuilder stringBuilder=new StringBuilder();
- stringBuilder.append("XML DATA");
- stringBuilder.append("\n-------------");
- NodeList nodeList=document.getElementsByTagName("place");
- for(int i=0; i<nodeList.getLength();i++){
- Node node = nodeList.item(i);
- if(node.getNodeType()==Node.ELEMENT_NODE){
- Element element = (Element)node;
- stringBuilder.append("\n Name:").append(getValue("name",element));
- stringBuilder.append("\n Latitude:").append(getValue("lat",element));
- stringBuilder.append("\n Longitude:").append(getValue("long",element));
- stringBuilder.append("\n
- Temperature:").append(getValue("temperature",element));
- stringBuilder.append("\n humidity").append(getValue("humidity",element));
- stringBuilder.append("\n----------");
- }
- }
- display.setText(stringBuilder.toString());
- }
- catch (Exception e){
- e.printStackTrace();
- Toast.makeText(MainActivity.this,"Error in reading XML
- FILE",Toast.LENGTH_LONG).show();
- }
- }
- public void parsejson(View V){
- String json;
- StringBuilder stringBuilder = new StringBuilder();
- try {
- InputStream is = getAssets().open("city.json");
- int size=is.available();
- byte[] buffer=new byte[size];
- is.read(buffer);
- json = new String(buffer, StandardCharsets.UTF_8);
- JSONArray jsonArray = new JSONArray(json);
- stringBuilder.append("JSON DATA");
- stringBuilder.append("\n--------");
- for(int i=0;i<jsonArray.length();i++){
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- stringBuilder.append("\n Name:").append(jsonObject.getString("name"));
- stringBuilder.append("\n Latidue:").append(jsonObject.getString("lat"));
- stringBuilder.append("\n Longitude:").append(jsonObject.getString("long"));
- stringBuilder.append("\n
- Temperature:").append(jsonObject.getString("temperature"));
- stringBuilder.append("\n Humidity:").append(jsonObject.getString("humidity"));
- stringBuilder.append("\n----------");
- }
- display.setText(stringBuilder.toString());
- is.close();
- }
- catch (Exception e){
- e.printStackTrace();
- Toast.makeText(MainActivity.this,"Error in reading JSON
- file",Toast.LENGTH_LONG).show();
- }
- }
- private String getValue(String tag,Element element){
- return
- element.getElementsByTagName(tag).item(0).getChildNodes().item(0).getNodeValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement