时间:2021-02-14 11:30:09 | 栏目:Android代码 | 点击:次
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
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等
/** Pull方式,创建 XML */ public String pullXMLCreate(){ StringWriter xmlWriter = new StringWriter(); Person []persons = new Person[3]; // 创建节点Person对象 persons[0] = new Person(1, "sunboy_2050", "http://blogcsdnnet/sunboy_2050"); persons[1] = new Person(2, "baidu", "http://wwwbaiducom"); persons[2] = new Person(3, "google", "http://wwwgooglecom"); try { // // 方式一:使用Android提供的实用工具类androidutilXml // XmlSerializer xmlSerializer = XmlnewSerializer(); // 方式二:使用工厂类XmlPullParserFactory的方式 XmlPullParserFactory factory = XmlPullParserFactorynewInstance(); XmlSerializer xmlSerializer = factorynewSerializer(); xmlSerializersetOutput(xmlWriter); // 保存创建的xml xmlSerializersetFeature("http://xmlpullorg/v1/doc/featureshtml#indent-output", true); // xmlSerializersetProperty("http://xmlpullorg/v1/doc/propertieshtml#serializer-indentation", " "); // 设置属性 // xmlSerializersetProperty("http://xmlpullorg/v1/doc/propertieshtml#serializer-line-separator", "\n"); xmlSerializerstartDocument("utf-8", null); // <?xml version='0' encoding='UTF-8' standalone='yes' ?> xmlSerializerstartTag("", "root"); xmlSerializerattribute("", "author", "homer"); xmlSerializerattribute("", "date", "2012-04-28"); int personsLen = personslength; for(int i=0; i<personsLen; i++) { xmlSerializerstartTag("", "person"); // 创建person节点 xmlSerializerstartTag("", "id"); xmlSerializertext(persons[i]getId()+""); xmlSerializerendTag("", "id"); xmlSerializerstartTag("", "name"); xmlSerializertext(persons[i]getName()); xmlSerializerendTag("", "name"); xmlSerializerstartTag("", "blog"); xmlSerializertext(persons[i]getBlog()); xmlSerializerendTag("", "blog"); xmlSerializerendTag("", "person"); } xmlSerializerendTag("", "root"); xmlSerializerendDocument(); } catch (XmlPullParserException e) { // XmlPullParserFactorynewInstance eprintStackTrace(); } catch (IllegalArgumentException e) { // xmlSerializersetOutput eprintStackTrace(); } catch (IllegalStateException e) { // xmlSerializersetOutput eprintStackTrace(); } catch (IOException e) { // xmlSerializersetOutput eprintStackTrace(); } catch (Exception e) { eprintStackTrace(); } savedXML(fileName, xmlWritertoString()); return xmlWritertoString(); }
运行结果:
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等
/** Pull方式,解析 XML */ public String pullXMLResolve(){ StringWriter xmlWriter = new StringWriter(); InputStream is = readXML(fileName); try { // // 方式一:使用Android提供的实用工具类androidutilXml // XmlPullParser xpp = XmlnewPullParser(); // 方式二:使用工厂类XmlPullParserFactory的方式 XmlPullParserFactory factory = XmlPullParserFactorynewInstance(); XmlPullParser xpp = factorynewPullParser(); xppsetInput(is, "utf-8"); List<Person> personsList = null; // 保存xml的person节点 Person person = null; StringBuffer xmlHeader = null; // 保存xml头部 String ele = null; // Element flag int eventType = xppgetEventType(); while(XmlPullParserEND_DOCUMENT != eventType) { switch (eventType) { case XmlPullParserSTART_DOCUMENT: personsList = new ArrayList<Person>(); // 初始化persons xmlHeader = new StringBuffer(); // 初始化xmlHeader break; case XmlPullParserSTART_TAG: if("root"equals(xppgetName())) { String attrAuthor = xppgetAttributeValue(0); String attrDate = xppgetAttributeValue(1); xmlHeaderappend("root")append("\t\t"); xmlHeaderappend(attrAuthor)append("\t"); xmlHeaderappend(attrDate)append("\n"); } else if("person"equals(xppgetName())) { person = new Person(); // 创建person实例 } else if("id"equals(xppgetName())) { ele = "id"; } else if("name"equals(xppgetName())) { ele = "name"; } else if("blog"equals(xppgetName())) { ele = "blog"; } else { ele = null; } break; case XmlPullParserTEXT: if(null != ele) { if("id"equals(ele)) { personsetId(IntegerparseInt(xppgetText())); } else if("name"equals(ele)) { personsetName(xppgetText()); } else if("blog"equals(ele)) { personsetBlog(xppgetText()); } } break; case XmlPullParserEND_TAG: if("person"equals(xppgetName())){ personsListadd(person); person = null; } ele = null; break; } eventType = xppnext(); // 下一个事件类型 } xmlWriterappend(xmlHeader); int personsLen = personsListsize(); for(int i=0; i<personsLen; i++) { xmlWriterappend(personsListget(i)toString()); } } catch (XmlPullParserException e) { // XmlPullParserFactorynewInstance eprintStackTrace(); } catch (Exception e) { eprintStackTrace(); } return xmlWritertoString(); }
运行结果:
4、Person类
请参见前面博客 Android 创建与解析XML(二)―― Dom方式 【4、Person类】