android 67 生成和解析xml
生成xml:
package com.itheima.createxml; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import com.itheima.createxml.domain.Message; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { List<Message> smsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //虚拟10条短信 smsList = new ArrayList<Message>(); for(int i = 0; i < 10; i++){ Message sms = new Message("小志好棒" + i, System.currentTimeMillis() + "", "138"+i+i, "1"); smsList.add(sms); } } public void click(View v){ //在内存中把xml备份短信的格式拼接出来,短信保存在外部,因为应用删除后要短信还在。 StringBuffer sb = new StringBuffer(); sb.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>"); sb.append("<messages>"); for (Message sms : smsList) {//拼接每一条短信sms sb.append("<sms>"); sb.append("<body>"); sb.append(sms.getBody()); sb.append("</body>"); sb.append("<date>"); sb.append(sms.getDate()); sb.append("</date>"); sb.append("<type>"); sb.append(sms.getType()); sb.append("</type>"); sb.append("<address>"); sb.append(sms.getAddress()); sb.append("</address>"); sb.append("</sms>"); } sb.append("</messages>"); File file = new File("sdcard/sms.xml"); try { FileOutputStream fos = new FileOutputStream(file); fos.write(sb.toString().getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
生成xml:
package com.itheima.xmlserializer; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlSerializer; import com.itheima.createxml.domain.Message; import android.os.Bundle; import android.app.Activity; import android.util.Xml; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { List<Message> smsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 虚拟10条短信 smsList = new ArrayList<Message>(); for (int i = 0; i < 10; i++) { Message sms = new Message("小志好棒" + i, System.currentTimeMillis() + "", "138" + i + i, "1"); smsList.add(sms); } } public void click(View v){ //使用xml序列化器生成xml文件 //1.拿到序列化器对象 XmlSerializer xs = Xml.newSerializer(); //2.初始化路径和文件名,生成在sd卡中, File file = new File("sdcard/sms2.xml"); try { FileOutputStream fos = new FileOutputStream(file); //enconding:指定用什么编码生成xml文件,安卓默认就是utf-8,所以几乎所有的编码都是utf-8, xs.setOutput(fos, "utf-8"); //utf-8指定头结点<message>中的enconding的值enconding="utf-8",true指定是否是独立的xml文件, xs.startDocument("utf-8", true); xs.startTag(null, "message");//开始节点的名字<message>,null为namespace, for (Message sms : smsList) { xs.startTag(null, "sms"); xs.startTag(null, "body"); xs.text(sms.getBody() + "<body>");//text表示文本节点 xs.endTag(null, "body"); xs.startTag(null, "date"); xs.text(sms.getDate()); xs.endTag(null, "date"); xs.startTag(null, "type"); xs.text(sms.getType()); xs.endTag(null, "type"); xs.startTag(null, "address"); xs.text(sms.getAddress()); xs.endTag(null, "address"); xs.endTag(null, "sms"); } xs.endTag(null, "message");//结束节点 //告诉序列化器,文件生成完毕 xs.endDocument(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
解析xml:
package com.itheima.pullparser; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import com.itheima.pullparser.domain.City; import android.os.Bundle; import android.app.Activity; import android.util.Xml; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { List<City> cityList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ //获取到src文件夹下的资源文件 InputStream is = getClassLoader().getResourceAsStream("weather.xml");//获取资源作为流 //拿到pull解析器对象 XmlPullParser xp = Xml.newPullParser(); //初始化 try { xp.setInput(is, "gbk");//第一个是输入流,gbk是解析编码,gbk是windows的默认编码格式,所以weather.xml的编码格式是gbk //获取当前节点的事件类型,通过事件类型的判断,我们可以知道当前节点是什么节点,从而确定我们应该做什么操作 //解析是一行一行的解析的, int type = xp.getEventType(); City city = null; while(type != XmlPullParser.END_DOCUMENT){//文档结束节点 //根据节点的类型,要做不同的操作 switch (type) { case XmlPullParser.START_TAG://开始节点 // 获取当前节点的名字 if("weather".equals(xp.getName())){ //创建city集合对象,用于存放city的javabean cityList = new ArrayList<City>(); } else if("city".equals(xp.getName())){ //创建city的javabean对象 city = new City(); } else if("name".equals(xp.getName())){ String name = xp.nextText();//获取当前节点的下一个节点的文本就是QQ,nextText并把指针移到当前节点的结束节点, city.setName(name); } else if("temp".equals(xp.getName())){ // 获取当前节点的下一个节点的文本 String temp = xp.nextText(); city.setTemp(temp); } else if("pm".equals(xp.getName())){ // 获取当前节点的下一个节点的文本 String pm = xp.nextText(); city.setPm(pm); } break; case XmlPullParser.END_TAG://结束节点,<name>QQ</name>遇到</name>什么都不做,遇到QQ文本节点什么都不做, if("city".equals(xp.getName())){ //把city的javabean放入集合中 cityList.add(city); } break; } //把指针移动到下一个节点,并返回该节点的事件类型,前一个结束节点和后一个开始节点会有一个换行符,换行符是一个文本节点, type = xp.next(); } for (City c : cityList) { System.out.println(c.toString()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /* <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <weather> <city> <name>QQ</name> <temp>5</temp> <pm>80</pm> </city> <city> <name>rr</name> <temp>-5</temp> <pm>800</pm> </city> <city> <name>����</name> <temp>12��</temp> <pm>60</pm> </city> </weather>*/