-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlparser.java
99 lines (85 loc) · 4.02 KB
/
xmlparser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.example.parsexmlandjson;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
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 org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
public class MainActivity extends AppCompatActivity {
Button json,xml;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
json = findViewById(R.id.json);
xml = findViewById(R.id.xml);
txt = findViewById(R.id.text);
xml.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {
try {
InputStream is = getAssets().open("city.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dc = documentBuilderFactory.newDocumentBuilder();
Document document = dc.parse(is);
txt.setText("XML data\n=================");
NodeList nodeList = document.getElementsByTagName("place");
Node node = nodeList.item(0);
if(node.getNodeType() == Node.ELEMENT_NODE){
Element element = (Element) node;
txt.setText(txt.getText()+"\n City Name : " + getValue("name",element));
txt.setText(txt.getText()+"\n Lat : " + getValue("lat",element));
txt.setText(txt.getText()+"\n Long : " + getValue("long",element));
txt.setText(txt.getText()+"\n Temperature : " + getValue("temp",element));
txt.setText(txt.getText()+"\n Humidity : " + getValue("humidity",element));
txt.setText(txt.getText() + "\n=================");
}
} catch (IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error parsing XML "+e.getCause(), Toast.LENGTH_LONG).show();
}
}
});
json.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {
try {
InputStream is = getAssets().open("city.json");
int size = is.available();
byte [] buff = new byte[size];
is.read(buff);
is.close();
String js = new String(buff, StandardCharsets.UTF_8);
txt.setText("JSON data\n=================");
JSONObject jsonObject = new JSONObject(js);
txt.setText(txt.getText()+"\nCity name : "+jsonObject.getString("name") + "\nLat : "+jsonObject.getString("lat") + "\nLat : "+jsonObject .getString("lang") + "\nTemperature : "+jsonObject.getString("temp") + "\nHumidity : "+jsonObject.getString("humidity") + "\n=================");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
});
}
private String getValue(String tag, Element element)
{
return
element.getElementsByTagName(tag).item(0).getChildNodes().item(0).getNodeValue();
}
}