1、Pull概述
Android系统中和创建XML相关的包为org.xmlpull.v1,在这个包中不仅提供了用于创建XML的 XmlSerializer,还提供了用来解析XML的Pull方式解析器 XmlPullParser
XmlSerializer没有像XmlPullParser那样提取XML事件,而是把它们推出到数据流OutputStream或Writer中。
XmlSerializer提供了很直观的API,即使用startDocument开始文档,endDocument结束文档,startTag开始元素,endTag结束元素,text添加文本等。
Pull方式创建XML,应用了标准xml构造器 org.xmlpull.v1.XmlSerializer来创建 XML ,org.xmlpull.v1.XmlPullParser来解析XML,需要导入以下内容
org.xmlpull.v1
org.xmlpull.v1.XmlPullParser;
org.xmlpull.v1.XmlPullParserException;
org.xmlpull.v1.XmlPullParserFactory;
org.xmlpull.v1.XmlSerializer;
sdk源码查看路径(google code)
Pull 创建和解析 XML 的效果图:
2、Pull 创建 XML
pull方式,创建xml是通过 XmlSerializer 类实现
首先,通过XmlSerializer得到创建xml的实例 xmlSerializer
接着,通过 xmlSerializer 设置输出 xmlSerializer.setOutput,xmlSerializer.startDocument("utf-8", null)设置xml属性等
然后,通过 xmlSerializer 创建 startDocument、startTag、text、endTag、endDocument等
Code
-
- public String pullXMLCreate(){
- StringWriter xmlWriter = new StringWriter();
-
- Person []persons = new Person[3];
- persons[0] = new Person(1, "sunboy_2050", "http://blog.csdn.net/sunboy_2050");
- persons[1] = new Person(2, "baidu", "http://www.baidu.com");
- persons[2] = new Person(3, "google", "http://www.google.com");
-
- try {
-
-
-
-
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- XmlSerializer xmlSerializer = factory.newSerializer();
-
- xmlSerializer.setOutput(xmlWriter);
-
- xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
-
-
- xmlSerializer.startDocument("utf-8", null);
-
- xmlSerializer.startTag("", "root");
- xmlSerializer.attribute("", "author", "homer");
- xmlSerializer.attribute("", "date", "2012-04-28");
-
- int personsLen = persons.length;
- for(int i=0; i<personsLen; i++) {
- xmlSerializer.startTag("", "person");
-
- xmlSerializer.startTag("", "id");
- xmlSerializer.text(persons[i].getId()+"");
- xmlSerializer.endTag("", "id");
-
- xmlSerializer.startTag("", "name");
- xmlSerializer.text(persons[i].getName());
- xmlSerializer.endTag("", "name");
-
- xmlSerializer.startTag("", "blog");
- xmlSerializer.text(persons[i].getBlog());
- xmlSerializer.endTag("", "blog");
-
- xmlSerializer.endTag("", "person");
- }
-
- xmlSerializer.endTag("", "root");
- xmlSerializer.endDocument();
-
- } catch (XmlPullParserException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- savedXML(fileName, xmlWriter.toString());
- return xmlWriter.toString();
- }
运行结果:
3、Pull 解析 XML
pull方式,解析xml是通过 XmlPullParser 类实现
首先,通过XmlPullParser得到解析xml的实例 xpp
接着,通过 xpp设置输入 xpp.setInput(is, "utf-8"),声明定义保存xml信息的数据结构(如:Person数组)
然后,通过 xpp 解析 START_DOCUMENT、START_TAG、TEXT、END_TAG、END_DOCUMENT等
Code
-
- public String pullXMLResolve(){
- StringWriter xmlWriter = new StringWriter();
-
- InputStream is = readXML(fileName);
- try {
-
-
-
-
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- XmlPullParser xpp = factory.newPullParser();
-
- xpp.setInput(is, "utf-8");
-
- List<Person> personsList = null;
- Person person = null;
- StringBuffer xmlHeader = null;
- String ele = null;
-
- int eventType = xpp.getEventType();
- while(XmlPullParser.END_DOCUMENT != eventType) {
- switch (eventType) {
- case XmlPullParser.START_DOCUMENT:
- personsList = new ArrayList<Person>();
- xmlHeader = new StringBuffer();
- break;
-
- case XmlPullParser.START_TAG:
- if("root".equals(xpp.getName())) {
- String attrAuthor = xpp.getAttributeValue(0);
- String attrDate = xpp.getAttributeValue(1);
- xmlHeader.append("root").append("\t\t");
- xmlHeader.append(attrAuthor).append("\t");
- xmlHeader.append(attrDate).append("\n");
- } else if("person".equals(xpp.getName())) {
- person = new Person();
- } else if("id".equals(xpp.getName())) {
- ele = "id";
- } else if("name".equals(xpp.getName())) {
- ele = "name";
- } else if("blog".equals(xpp.getName())) {
- ele = "blog";
- } else {
- ele = null;
- }
- break;
-
- case XmlPullParser.TEXT:
- if(null != ele) {
- if("id".equals(ele)) {
- person.setId(Integer.parseInt(xpp.getText()));
- } else if("name".equals(ele)) {
- person.setName(xpp.getText());
- } else if("blog".equals(ele)) {
- person.setBlog(xpp.getText());
- }
- }
- break;
-
- case XmlPullParser.END_TAG:
- if("person".equals(xpp.getName())){
- personsList.add(person);
- person = null;
- }
- ele = null;
- break;
- }
-
- eventType = xpp.next();
- }
-
- xmlWriter.append(xmlHeader);
- int personsLen = personsList.size();
- for(int i=0; i<personsLen; i++) {
- xmlWriter.append(personsList.get(i).toString());
- }
-
- } catch (XmlPullParserException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return xmlWriter.toString();
- }
运行结果:
4、Person类
请参见前面博客 Android 创建与解析XML(二)—— Dom方式 【4、Person类】
代码下载