@H_403_1@#1.@H_403_1@使用序列化器生成一个@H_403_1@xml@H_403_1@文件
@H_403_1@//1,@H_403_1@初始化一个@H_403_1@xml@H_403_1@文件的序列化器
@H_403_1@XmlSerializerserializer=Xml.newSerializer();
@H_403_1@//2.@H_403_1@初始化序列器参数
@H_403_1@Filefile=newFile(Environment.getExternalStorageDirectory(),"backup.xml");
@H_403_1@FileOutputStreamfos=newFileOutputStream(file);
@H_403_1@serializer.setOutput(fos,"UTF-8");
@H_403_1@//3.@H_403_1@开始写@H_403_1@xml@H_403_1@文件@H_403_1@.
@H_403_1@serializer.startDocument("UTF-8",true);
@H_403_1@serializer.startTag(null,"smss");
@H_403_1@for(SmsInfoinfo:smsInfos){
@H_403_1@//@H_403_1@开始写@H_403_1@sms@H_403_1@节点
@H_403_1@serializer.startTag(null,"sms");
@H_403_1@//@H_403_1@开始写@H_403_1@body@H_403_1@节点
@H_403_1@serializer.startTag(null,"body");
@H_403_1@serializer.text(info.getBody());
@H_403_1@serializer.endTag(null,"body");
@H_403_1@//@H_403_1@开始写@H_403_1@address@H_403_1@节点
@H_403_1@serializer.startTag(null,"address");
@H_403_1@serializer.text(info.getAddress());
@H_403_1@serializer.endTag(null,"address");
@H_403_1@//@H_403_1@开始写@H_403_1@data@H_403_1@节点
@H_403_1@serializer.startTag(null,"date");
@H_403_1@serializer.text(info.getDate()+"");
@H_403_1@serializer.endTag(null,"date");
@H_403_1@serializer.endTag(null,"sms");
@H_403_1@}
@H_403_1@serializer.endTag(null,"smss");
@H_403_1@serializer.endDocument();
@H_403_1@fos.close();
@H_403_1@Toast.makeText(this,"@H_403_1@备份短信成功@H_403_1@",0).show();
@H_403_1@}catch(Exceptione){
@H_403_1@e.printStackTrace();
@H_403_1@Toast.makeText(this,"@H_403_1@备份短信失败@H_403_1@",0).show();
@H_403_1@}
@H_403_1@#12.@H_403_1@使用@H_403_1@pull@H_403_1@解析@H_403_1@xml@H_403_1@格式的数据@H_403_1@(@H_403_1@重要@H_403_1@)
@H_403_1@publicclassMainActivityextendsActivity{
@H_403_1@@Override
@H_403_1@protectedvoidonCreate(BundlesavedInstanceState){
@H_403_1@super.onCreate(savedInstanceState);
@H_403_1@//@H_403_1@设置@H_403_1@activity@H_403_1@显示的布局
@H_403_1@setContentView(R.layout.activity_main);
@H_403_1@TextViewtv_info=(TextView)findViewById(R.id.tv_info);
@H_403_1@StringBuildersb=newStringBuilder();
@H_403_1@try{
@H_403_1@//@H_403_1@获取我们解析出来的天气信息
@H_403_1@List<Channel>channels=WeatherService.getAllWeatherInfos(getClass().getClassLoader().getResourceAsStream("weather.xml"));
@H_403_1@for(Channelchannel:channels){
@H_403_1@sb.append(channel.toString());
@H_403_1@sb.append("\n");
@H_403_1@}
@H_403_1@//@H_403_1@把解析出来的天气信息设置到@H_403_1@textview@H_403_1@上
@H_403_1@tv_info.setText(sb.toString());
@H_403_1@}catch(Exceptione){
@H_403_1@e.printStackTrace();
@H_403_1@Toast.makeText(this,"@H_403_1@解析天气信息失败@H_403_1@",0).show();
@H_403_1@}
@H_403_1@}
@H_403_1@}
@H_403_1@---------------------------------------------------------------
@H_403_1@publicclassWeatherService{
@H_403_1@/**
@H_403_1@*@H_403_1@解析服务器返回的数据获取天气信息
@H_403_1@*@paramis@H_403_1@服务器返回的包含天气信息的流@H_403_1@(xml)
@H_403_1@*@return
@H_403_1@*/
@H_403_1@publicstaticList<Channel>getAllWeatherInfos(InputStreamis)throwsException{
@H_403_1@List<Channel>channels=null;
@H_403_1@Channelchannel=null;
@H_403_1@//1.@H_403_1@获取@H_403_1@xml@H_403_1@解析器
@H_403_1@XmlPullParserparser=Xml.newPullParser();
@H_403_1@//2.@H_403_1@设置@H_403_1@xml@H_403_1@解析器的参数
@H_403_1@parser.setInput(is,"utf-8");
@H_403_1@//3.@H_403_1@开始解析@H_403_1@xml@H_403_1@文件@H_403_1@.
@H_403_1@inttype=parser.getEventType();//@H_403_1@获取当前的事件的类型
@H_403_1@while(type!=XmlPullParser.END_DOCUMENT){//@H_403_1@需要让@H_403_1@pull@H_403_1@解析器解析到文件的末尾
@H_403_1@switch(type){
@H_403_1@caseXmlPullParser.START_TAG:
@H_403_1@if("weather".equals(parser.getName())){//@H_403_1@总的开始节点
@H_403_1@channels=newArrayList<Channel>();//@H_403_1@初始化集合
@H_403_1@}elseif("channel".equals(parser.getName())){//@H_403_1@某个城市的信息开始了@H_403_1@.
@H_403_1@channel=newChannel();
@H_403_1@//@H_403_1@获取到@H_403_1@id@H_403_1@的属性值
@H_403_1@Stringid=parser.getAttributeValue(0);
@H_403_1@channel.setId(Integer.parseInt(id));
@H_403_1@//@H_403_1@解析@H_403_1@city@H_403_1@节点
@H_403_1@}elseif("city".equals(parser.getName())){
@H_403_1@Stringcity=parser.nextText();
@H_403_1@channel.setCity(city);
@H_403_1@}elseif("temp".equals(parser.getName())){
@H_403_1@Stringtemp=parser.nextText();
@H_403_1@channel.setTemp(temp);
@H_403_1@}elseif("wind".equals(parser.getName())){
@H_403_1@Stringwind=parser.nextText();
@H_403_1@channel.setWind(wind);
@H_403_1@//@H_403_1@解析@H_403_1@pm250@H_403_1@节点
@H_403_1@}elseif("pm250".equals(parser.getName())){
@H_403_1@Stringpm250=parser.nextText();
@H_403_1@channel.setPm250(Integer.parseInt(pm250));
@H_403_1@}
@H_403_1@break;
@H_403_1@//@H_403_1@判断@H_403_1@xml@H_403_1@的结束节点
@H_403_1@caseXmlPullParser.END_TAG:
@H_403_1@if("channel".equals(parser.getName())){
@H_403_1@//@H_403_1@把解析的内容加入到集合中
@H_403_1@channels.add(channel);
@H_403_1@channel=null;
@H_403_1@}
@H_403_1@break;
@H_403_1@}
@H_403_1@type=parser.next();
@H_403_1@}
@H_403_1@is.close();
@H_403_1@returnchannels;//@H_403_1@把所有的频道的集合返回回去
@H_403_1@}
@H_403_1@}