话不多说,上代码
第一步新建项目,编写activity_main.xml
:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/weather"
tools:context=".MainActivity">
:id="@+id/tv_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/tv_weather"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/tv_weather"
android:layout_marginTop="39dp"
android:text="上海"
android:textSize="50sp"/>
:id="@+id/iv_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignLeft="@+id/ll_btn"
android:layout_alignStart="@+id/ll_btn"
android:layout_below="@+id/tv_city"
android:layout_marginLeft="44dp"
android:layout_marginStart="44dp"
android:layout_marginTop="42dp"
android:paddingBottom="5dp"
android:src="@mipmap/ic_launcher"/>
:id="@+id/tv_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/iv_icon"
android:layout_below="@+id/iv_icon"
android:layout_marginRight="15dp"
android:layout_marginTop="18dp"
android:gravity="center"
android:text="多云"
android:textSize="18sp"/>
:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/iv_icon"
android:layout_marginLeft="39dp"
android:layout_marginStart="39dp"
android:layout_toEndOf="@+id/iv_icon"
android:layout_toRightOf="@+id/iv_icon"
android:gravity="center"
android:orientation="vertical">
:id="@+id/tv_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="-7℃"
android:textSize="22sp"/>
:id="@+id/tv_wind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="风力:3级"
android:textSize="18sp"/>
:id="@+id/tv_pm"
android:layout_width="73dp"
android:layout_height="wrap_content"
android:text="pm"
android:textSize="18sp"/>
>
:id="@+id/ll_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
>
>
第二步创建new->res->raw ->weather.xml文件
提示:如果新建完毕,引用过程中 raw标红解决办法:【file】->【Invalidate and restart】即可
<?xml version="1.0" encoding="utf-8"?>
>
>
>20℃/30℃ >
>晴天多云 >
>上海 >
>80 >
>1级 >
>
>
>26℃/32℃ >
>晴天 >
>北京 >
>98 >
>3级 >
>
>
>15℃/24℃ >
>多云 >
>广州 >
>30 >
>5级 >
>
>
第三步创建WeatherInfo类
package com.hh.parsexmldemo01;
public class WeatherInfo {
private String id;
private String temp;
private String weather;
private String name;
private String pm;
private String wind;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPm() {
return pm;
}
public void setPm(String pm) {
this.pm = pm;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
@Override
public String toString(){
System.out.println("id:"+getId()+"temp:"+getTemp()+"name:"+getName()+"pm:"+getPm()+"weather"+getWeather()+"wind:"+getWind());
return null;
}
}
第四步创建WeatherServiceDom,WeatherServiceSax工具类
package com.hh.parsexmldemo01;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
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.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
public class WeatherServiceDom {
public static List<WeatherInfo> getInfosFromXML (InputStream is) throws ParserConfigurationException, IOException, SAXException {
List<WeatherInfo> weatherInfos = null;
WeatherInfo weatherInfo = null;
//建立DocumentBuilderFactor,用于获得DocumentBuilder对象:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.建立DocumentBuidler:
DocumentBuilder builder = factory.newDocumentBuilder();
//3.建立Document对象,获取树的入口:
Document doc = builder.parse(is);
//4.建立NodeList:
NodeList node = doc.getElementsByTagName("city");
weatherInfos = new ArrayList<WeatherInfo>();
//5.进行xml信息获取
for(int i=0;i<node.getLength();i++){
Element e = (Element)node.item(i);
weatherInfo=new WeatherInfo();
//String id=e.getAttribute("id");
//weatherInfo.setId(id);
String temp=e.getElementsByTagName("temp").item(0).getFirstChild().getNodeValue();
weatherInfo.setTemp(temp);
String weather=e.getElementsByTagName("weather").item(0).getFirstChild().getNodeValue();
weatherInfo.setWeather(weather);
String name=e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
weatherInfo.setName(name);
String pm=e.getElementsByTagName("pm").item(0).getFirstChild().getNodeValue();
weatherInfo.setPm(pm);
String wind=e.getElementsByTagName("wind").item(0).getFirstChild().getNodeValue();
weatherInfo.setWind(wind);
weatherInfos.add(weatherInfo);
}
weatherInfo.toString();
return weatherInfos;
}
}
package com.hh.parsexmldemo01;
import android.util.Log;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class WeatherServiceSax {
//创建一个LIst容器存放WeatherInfo对象
static List<WeatherInfo> weatherInfos = null;
//存放标签名
static String tagName=null;
//定义一个对象
static WeatherInfo weatherInfo = null;
public static List<WeatherInfo> getInfosFromXML () throws ParserConfigurationException, SAXException, IOException {
//1.先创建解析器工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//2.利用工厂创建一个解析器
SAXParser saxParser = factory.newSAXParser();
//3.调用解析方法,解析文件,该方法有两个参数,第一个参数是要解析的文件,第二个参数是处理类
try{
saxParser.parse("raw/weather1.xml",new MyDefaultHandler());
}catch(SAXException e){
e.printStackTrace();
}
return weatherInfos;
}
static class MyDefaultHandler extends DefaultHandler {
@Override
public void startDocument()throws SAXException{
Log.v("sax","startDocument开始解析...");
weatherInfos = new ArrayList<WeatherInfo>();
}
@Override
public void endDocument() throws SAXException {
Log.v("sax","endDocument解析完毕...");
}
//开始标签
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equals("city")) { //拿到一个Employee对象
weatherInfo = new WeatherInfo(); //就创建一个对象
String id = attributes.getValue("id"); //获取属性值
}else {
tagName = qName;
}
}
//解析一个weatherInfo结束标签
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equals("city")) { //此时说明一个对象已经解析完,我们就将当前对象存放到容器中
weatherInfos.add(weatherInfo);
}else {
//因为SAX解析也会识别空文本,所以每次遇到尾标签,需要将tagName值空,避免,将当前的值被空文本给覆盖掉
tagName = null;
}
}
//当前文本内容
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
//构建当前读取到文本内容
String msg = new String(ch,start,length);
//tagName存放这当前的qName,判断当前拿取到的是哪个值
if(tagName.equals("temp")) {
weatherInfo.setTemp(msg);
}else if(tagName.equals("weather")) {
weatherInfo.setWeather(msg);
}else if(tagName.equals("name")) {
weatherInfo.setName(msg);
}else if(tagName.equals("pm")) {
weatherInfo.setPm(msg);
}else if(tagName.equals("wind")) {
weatherInfo.setWind(msg);
}
}
}
}
第五步编写界面交互代码
package com.hh.parsexmldemo01;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
private TextView tvCity;
private TextView tvWeather;
private TextView tvTemp;
private TextView tvWind;
private TextView tvPm;
private ImageView ivIcon;
private Map<String, String> map;
private List<Map<String, String>> list;
private String temp, weather, name, pm, wind;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化文本控件
initView();
try {
//读取weather1.xml文件
InputStream is = this.getResources().openRawResource(R.raw.weather1);
//把每个城市的天气信息集合存到weatherInfos中
List<WeatherInfo> weatherInfos = WeatherServiceDom.getInfosFromXML(is);
//List weatherInfos = WeatherServiceSax.getInfosFromXML();
//循环读取weatherInfos中的每一条数据
list = new ArrayList<Map<String, String>>();
for (WeatherInfo info : weatherInfos) {
map = new HashMap<String, String>();
map.put("temp", info.getTemp());
map.put("weather", info.getWeather());
map.put("name", info.getName());
map.put("pm", info.getPm());
map.put("wind", info.getWind());
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "解析信息失败", Toast.LENGTH_SHORT).show();
}
//自定义getMap()方法,显示天气信息到文本控件中,默认显示北京的天气
getMap(1, R.drawable.sun);
}
private void initView() {
tvCity = (TextView) findViewById(R.id.tv_city);
tvWeather = (TextView) findViewById(R.id.tv_weather);
tvTemp = (TextView) findViewById(R.id.tv_temp);
tvWind = (TextView) findViewById(R.id.tv_wind);
tvPm = (TextView) findViewById(R.id.tv_pm);
ivIcon = (ImageView) findViewById(R.id.iv_icon);
findViewById(R.id.btn_sh).setOnClickListener(this);
findViewById(R.id.btn_bj).setOnClickListener(this);
findViewById(R.id.btn_gz).setOnClickListener(this);
}
@Override
public void onClick(View v) { //按钮的点击事件
switch (v.getId()) {
case R.id.btn_sh:
getMap(0, R.drawable.cloud_sun);
break;
case R.id.btn_bj:
getMap(1, R.drawable.sun);
break;
case R.id.btn_gz:
getMap(2, R.drawable.clouds);
break;
}
}
//将城市天气信息分条展示到界面上
private void getMap(int number, int iconNumber) {
Map<String, String> cityMap = list.get(number);
temp = cityMap.get("temp");
weather = cityMap.get("weather");
name = cityMap.get("name");
pm = cityMap.get("pm");
wind = cityMap.get("wind");
tvCity.setText(name);
tvWeather.setText(weather);
tvTemp.setText("" + temp);
tvWind.setText("风力 : " + wind);
tvPm.setText("pm: " + pm);
ivIcon.setImageResource(iconNumber);
}
}