使用序列化器生成一个xml文件 //1,初始化一个xml文件的序列化器 XmlSerializer serializer = Xml.newSerializer(); //2.初始化序列器参数 File file = new File(Environment.getExternalStorageDirectory(),"backup.xml"); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos, "UTF-8"); //3.开始写xml文件. serializer.startDocument("UTF-8", true); serializer.startTag(null, "smss"); for(SmsInfo info : smsInfos){ //开始写sms节点 serializer.startTag(null, "sms"); //开始写body节点 serializer.startTag(null, "body"); serializer.text(info.getBody()); //body节点结束 serializer.endTag(null, "body"); //开始写address节点 serializer.startTag(null, "address"); serializer.text(info.getAddress()); serializer.endTag(null, "address"); //开始写data节点 serializer.startTag(null, "date"); serializer.text(info.getDate()+""); serializer.endTag(null, "date"); // sms节点结束 serializer.endTag(null, "sms"); } //smss根节点结束 serializer.endTag(null, "smss"); //xml 结束 serializer.endDocument(); fos.close(); Toast.makeText(this, "备份短信成功", 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "备份短信失败", 0).show(); } #12.使用pull解析xml格式的数据 (重要) public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置activity显示的布局 setContentView(R.layout.activity_main); TextView tv_info = (TextView) findViewById(R.id.tv_info); StringBuilder sb = new StringBuilder(); try { //获取我们解析出来的天气信息 List<Channel> channels = WeatherService.getAllWeatherInfos(getClass().getClassLoader().getResourceAsStream("weather.xml")); for(Channel channel : channels){ sb.append(channel.toString()); sb.append("\n"); } //把解析出来的天气信息设置到textview上 tv_info.setText(sb.toString()); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "解析天气信息失败", 0).show(); } } } --------------------------------------------------------------- public class WeatherService { /** * 解析服务器返回的数据 获取天气信息 * @param is 服务器返回的包含天气信息的流 (xml) * @return */ public static List<Channel> getAllWeatherInfos(InputStream is) throws Exception{ List<Channel> channels = null; Channel channel = null; //1.获取xml解析器 XmlPullParser parser = Xml.newPullParser(); //2.设置xml解析器的参数 parser.setInput(is, "utf-8"); //3.开始解析xml文件. int type = parser.getEventType();// 获取当前的事件的类型 while (type!=XmlPullParser.END_DOCUMENT){ //需要让pull解析器解析到文件的末尾 switch (type) { case XmlPullParser.START_TAG: if("weather".equals(parser.getName())){//总的开始节点 channels = new ArrayList<Channel>(); //初始化集合 }else if("channel".equals(parser.getName())){//某个城市的信息开始了. channel = new Channel(); //获取到id的属性值 String id = parser.getAttributeValue(0); channel.setId(Integer.parseInt(id)); //解析city节点 }else if("city".equals(parser.getName())){ String city = parser.nextText(); channel.setCity(city); //解析温度节点 }else if("temp".equals(parser.getName())){ String temp = parser.nextText(); channel.setTemp(temp); //解析风力节点 }else if("wind".equals(parser.getName())){ String wind = parser.nextText(); channel.setWind(wind); //解析pm250节点 }else if("pm250".equals(parser.getName())){ String pm250 = parser.nextText(); channel.setPm250(Integer.parseInt(pm250)); } break; //判断xml的结束节点 case XmlPullParser.END_TAG: if("channel".equals(parser.getName())){ //把解析的内容加入到集合中 channels.add(channel); channel = null; } break; } type = parser.next(); } is.close(); return channels;//把所有的频道的集合返回回去 } }
时间: 2024-10-17 16:01:57