天天看點

Android Studio解析天氣XML檔案之SAX解析,Dom解析。

話不多說,上代碼

第一步建立項目,編寫activity_main.xml

<RelativeLayout xmlns: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">
    <TextView
        android: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"/>
    <ImageView
        android: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"/>
    <TextView
        android: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"/>
    <LinearLayout
        android: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">
        <TextView
            android: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"/>
        <TextView
            android:id="@+id/tv_wind"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="風力:3級"
            android:textSize="18sp"/>
        <TextView
            android:id="@+id/tv_pm"
            android:layout_width="73dp"
            android:layout_height="wrap_content"
            android:text="pm"
            android:textSize="18sp"/>
    </LinearLayout>
    <LinearLayout
        android: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">
        <Button
            android:id="@+id/btn_bj"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="北京"/>
        <Button
            android:id="@+id/btn_sh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上海"/>
        <Button
            android:id="@+id/btn_gz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="廣州"/>
    </LinearLayout>
</RelativeLayout>


           

第二步建立new->res->raw ->weather.xml檔案

提示:如果建立完畢,引用過程中 raw标紅解決辦法:【file】->【Invalidate and restart】即可

<?xml version="1.0" encoding="utf-8"?>
<infos>
    <city id="sh">
        <temp>20℃/30℃</temp>
        <weather>晴天多雲</weather>
        <name>上海</name>
        <pm>80</pm>
        <wind>1級</wind>
    </city>
    <city id="bj">
        <temp>26℃/32℃</temp>
        <weather>晴天</weather>
        <name>北京</name>
        <pm>98</pm>
        <wind>3級</wind>
    </city>
    <city id="gz">
        <temp>15℃/24℃</temp>
        <weather>多雲</weather>
        <name>廣州</name>
        <pm>30</pm>
        <wind>5級</wind>
    </city>
</infos>

           

第三步建立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<WeatherInfo> 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);
    }
}


           

運作結果

Android Studio解析天氣XML檔案之SAX解析,Dom解析。
Android Studio解析天氣XML檔案之SAX解析,Dom解析。
Android Studio解析天氣XML檔案之SAX解析,Dom解析。