Android -- 创建XML文件对象及其序列化, pull解析XML文件
生活随笔
收集整理的這篇文章主要介紹了
Android -- 创建XML文件对象及其序列化, pull解析XML文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 創建XML文件對象及其序列化
示例代碼:(模擬以xml格式備份短信到SD卡)
SmsInfo.java, bean對象
/*** 短信的業務bean* @author Administrator**/ public class SmsInfo {private String body;private String number;private int type;private long id;public long getId() {return id;}public void setId(long id) {this.id = id;}public SmsInfo() {}public SmsInfo(String body, String number, int type,long id) {this.body = body;this.number = number;this.type = type;this.id = id;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}public int getType() {return type;}public void setType(int type) {this.type = type;}}SmsUtils.java 工具類
public class SmsUtils {/*** 短信備份的工具方法* @param file 短信備份到哪個文件里面* @param smsInfos 要備份的短信對象的集合.*/public static void backUpSms(File file, List<SmsInfo> smsInfos) throws Exception{//xml文件的序列號器 幫助生成一個xml文件FileOutputStream fos = new FileOutputStream(file);//1.獲取到xml的序列號器XmlSerializer serializer = Xml.newSerializer();//2.序列化器的初始化serializer.setOutput(fos, "utf-8"); //文件的編碼方式 utf-8//3.創建xml文件,編碼和是否獨立,如果獨立,一個xml文件 就會包含所有信息 serializer.startDocument("utf-8", true);serializer.startTag(null, "smss");//循環的把所有的短信數據都寫到 xml文件里面for(SmsInfo info: smsInfos){serializer.startTag(null, "sms");serializer.attribute(null, "id", String.valueOf(info.getId()));serializer.startTag(null, "body");serializer.text(info.getBody());serializer.endTag(null, "body");serializer.startTag(null, "type");serializer.text(info.getType()+"");serializer.endTag(null, "type");serializer.startTag(null, "number");serializer.text(info.getNumber());serializer.endTag(null, "number");serializer.endTag(null, "sms");}serializer.endTag(null, "smss");serializer.endDocument();fos.flush();fos.close();} }MainActivity.java
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);List<SmsInfo> smsInfos = new ArrayList<SmsInfo>();//模擬創建兩個短信數據的對象.SmsInfo sms1 = new SmsInfo("你好啊 短信1", "5556", 1,9999);SmsInfo sms2 = new SmsInfo("你好啊 短信2", "5558", 1,8888);smsInfos.add(sms1);smsInfos.add(sms2);File file = new File(Environment.getExternalStorageDirectory(),"back.xml");try {SmsUtils.backUpSms(file, smsInfos);Toast.makeText(this, "備份短信成功", 0).show();} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "備份短信失敗", 0).show();}} }
2. pull解析XML文件
示例代碼:(模擬解析xml格式的天氣情況)
weather.xml 需要解析的XML文件
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <weather><day id="1"><wendu>18</wendu><wind>5</wind><type>晴</type></day><day id="2"><wendu>16</wendu><wind>3</wind><type>雨</type></day> </weather>Weather.java, bean
public class Weather {private int wendu;private int wind;private String type;private int id;public int getWendu() {return wendu;}public void setWendu(int wendu) {this.wendu = wendu;}public int getWind() {return wind;}public void setWind(int wind) {this.wind = wind;}public String getType() {return type;}public void setType(String type) {this.type = type;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "天氣信息 [溫度=" + wendu + ", 風力=" + wind + "級 , 天氣狀況=" + type+ ", 未來第=" + id + "天]";} }WeatherService.java,業務類
public class WeatherService {/*** 解析獲取天氣信息* * @param is* 天氣信息xml文件對應的流* @return* @throws Exception*/public static List<Weather> getWeather(InputStream is) throws Exception {// 解析 天氣的xml文件.// 1.獲取到一個xml文件的解析器.XmlPullParser parser = Xml.newPullParser();// 2.初始化解析器.parser.setInput(is, "utf-8");// 3.解析xml文件.// 得到當前解析條目的節點類型.int eventType = parser.getEventType(); // 第一次被調用的時候 定位在xml開頭List<Weather> weatherInfos = null;Weather weatherInfo = null;while (eventType != XmlPullParser.END_DOCUMENT) {// 需要 不停的讓 解析器解析下一個節點switch (eventType) {case XmlPullParser.START_TAG:if ("weather".equals(parser.getName())) {// 發現開始節點 為weather 創建集合weatherInfos = new ArrayList<Weather>();} else if ("day".equals(parser.getName())) {// 發現一個新的日期 對應的天氣weatherInfo = new Weather();String id = parser.getAttributeValue(0);weatherInfo.setId(Integer.parseInt(id));} else if ("wendu".equals(parser.getName())) {String wendu = parser.nextText();weatherInfo.setWendu(Integer.parseInt(wendu));} else if ("wind".equals(parser.getName())) {String wind = parser.nextText();weatherInfo.setWind(Integer.parseInt(wind));} else if ("type".equals(parser.getName())) {String type = parser.nextText();weatherInfo.setType(type);}break;case XmlPullParser.END_TAG:if ("day".equals(parser.getName())) {weatherInfos.add(weatherInfo);}break;}eventType = parser.next();// 控制解析器 解析下一個節點}is.close();return weatherInfos;} }MainActivity.java
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.tv_weather);try {StringBuilder sb = new StringBuilder();List<Weather> weatherinfos = WeatherService.getWeather(getClassLoader().getResourceAsStream("weather.xml"));for(Weather weather : weatherinfos){sb.append(weather.toString());sb.append("\n");}tv.setText(sb.toString());} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "解析天氣信息失敗", 0).show();} } }
?
轉載于:https://www.cnblogs.com/xj626852095/p/3647985.html
總結
以上是生活随笔為你收集整理的Android -- 创建XML文件对象及其序列化, pull解析XML文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国地区三级联动下拉菜单的实现
- 下一篇: Ubuntu 10.10, 11.04,