android源代码的编译过程,Android笔记 解析xml文件demo

 2023-09-22 阅读 18 评论 0

摘要:1编写布局文件 <LinearLayout 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"a

1编写布局文件

<LinearLayout 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:orientation="vertical"tools:context=".MainActivity" ><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="click"android:text="解析"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/text"android:text="@string/hello_world" /></LinearLayout>

2编写xml文件 这里只是说明怎么解析 因此没有从网络获取

<?xml version="1.0" encoding="utf-8"?>
<infos><city id="1"><temp>20℃/30℃</temp><weather>5月20日 多云转阴</weather><wind>南风三到四级</wind><name>上海</name><pm>200</pm></city><city id="2"><temp>20℃/30℃</temp><weather>5月20日 多云转阴</weather><wind>南风7到8级</wind><name>北京</name><pm>800</pm></city><city id="3"><temp>6℃/10℃</temp><weather>5月20日 多云转阴</weather><wind>南风7到8级</wind><name>哈尔滨</name><pm>100</pm></city></infos>

文件保存在assert文件夹下

android源代码的编译过程、

3根据xml文件写一个weatherBean

package com.example.xmljiexi.bean;public class WeatherBean {private int id;private String temp;private String weather;private String wind;private String name;private int pm;public int getId() {return id;}public void setId(int 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 getWind() {return wind;}public void setWind(String wind) {this.wind = wind;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPm() {return pm;}public void setPm(int pm) {this.pm = pm;}@Overridepublic String toString() {return "WeatherBean [id=" + id + ", temp=" + temp + ", weather="+ weather + ", wind=" + wind + ", name=" + name + ", pm=" + pm+ "]";}}

4写一个工具类

package com.example.xmljiexi.tool;import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;
import com.example.xmljiexi.bean.WeatherBean;public class Parser {// 返回一个集合 集合里是每一个城市的情况// 需要一个参数 把xml文件转化成流public static List<WeatherBean> getParse(InputStream is)throws XmlPullParserException, IOException {List<WeatherBean> list = null;WeatherBean weatherBean = null;// 初始化XmlPullParser parser = Xml.newPullParser();// 设置信息所在文件流和编码parser.setInput(is, "utf-8");// 获取事件类型 文档开始--0 文档结束--1 标签开始--2 标签结束--3 text标签外的内容如空格 回车 等--4int type = parser.getEventType();// 判断文档是否结束读取while (type != XmlPullParser.END_DOCUMENT) {// 判断事件类型switch (type) {case XmlPullParser.START_DOCUMENT:// 不作任何处理break;// 事件为标签开始case XmlPullParser.START_TAG:// 获取当前事件对应的名称if ("infos".equals(parser.getName())) {//进入文档,即将获得具体城市信息list = new ArrayList<WeatherBean>();} else if ("city".equals(parser.getName())) {weatherBean =new WeatherBean();String id = parser.getAttributeValue(0);weatherBean.setId(Integer.parseInt(id));} else if ("temp".equals(parser.getName())) {String temp = parser.nextText();weatherBean.setTemp(temp);} else if ("weather".equals(parser.getName())) {String weather = parser.nextText();weatherBean.setWeather(weather);} else if ("wind".equals(parser.getName())) {String wind = parser.nextText();weatherBean.setWind(wind);} else if ("pm".equals(parser.getName())) {String pm = parser.nextText();weatherBean.setPm(Integer.parseInt(pm));} else if ("name".equals(parser.getName())) {String name = parser.nextText();weatherBean.setName(name);}break;case XmlPullParser.END_TAG:if ("city".equals(parser.getName())) {//一个城市信息解析完毕list.add(weatherBean);weatherBean = null;}break;default:break;}type = parser.next();}return list;}
}

5主界面

android studio编译。

package com.example.xmljiexi;import java.io.IOException;
import java.io.InputStream;
import java.util.List;import com.example.xmljiexi.bean.WeatherBean;
import com.example.xmljiexi.tool.Parser;import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;public class MainActivity extends Activity {TextView textView;AssetManager assetManager;List<WeatherBean> list;StringBuffer stringBuffer =new StringBuffer();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView =(TextView) findViewById(R.id.text);assetManager = getAssets();}public void click(View view) {//step1 获取asserts文件中chj.xml内容 要转化成流try {InputStream is = assetManager.open("chj.xml");list = Parser.getParse(is);for(int i = 0; i<list.size() ; i++){WeatherBean weatherBean = list.get(i);stringBuffer.append(weatherBean.toString()+"\n");}textView.setText(stringBuffer);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}



版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/86288.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息