Android 创建与解析XML(四)—— Pull方式
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
- /** Pull方式,创建 XML */
- public String pullXMLCreate(){
- StringWriter xmlWriter = new StringWriter();
- Person []persons = new Person[3]; // 创建节点Person对象
- 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 {
- // // 方式一:使用Android提供的实用工具类android.util.Xml
- // XmlSerializer xmlSerializer = Xml.newSerializer();
- // 方式二:使用工厂类XmlPullParserFactory的方式
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- XmlSerializer xmlSerializer = factory.newSerializer();
- xmlSerializer.setOutput(xmlWriter); // 保存创建的xml
- xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
- // xmlSerializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " "); // 设置属性
- // xmlSerializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");
- xmlSerializer.startDocument("utf-8", null); // <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
- 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"); // 创建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) { // XmlPullParserFactory.newInstance
- e.printStackTrace();
- } catch (IllegalArgumentException e) { // xmlSerializer.setOutput
- e.printStackTrace();
- } catch (IllegalStateException e) { // xmlSerializer.setOutput
- e.printStackTrace();
- } catch (IOException e) { // xmlSerializer.setOutput
- 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
- /** Pull方式,解析 XML */
- public String pullXMLResolve(){
- StringWriter xmlWriter = new StringWriter();
- InputStream is = readXML(fileName);
- try {
- // // 方式一:使用Android提供的实用工具类android.util.Xml
- // XmlPullParser xpp = Xml.newPullParser();
- // 方式二:使用工厂类XmlPullParserFactory的方式
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- XmlPullParser xpp = factory.newPullParser();
- xpp.setInput(is, "utf-8");
- List<Person> personsList = null; // 保存xml的person节点
- Person person = null;
- StringBuffer xmlHeader = null; // 保存xml头部
- String ele = null; // Element flag
- int eventType = xpp.getEventType();
- while(XmlPullParser.END_DOCUMENT != eventType) {
- switch (eventType) {
- case XmlPullParser.START_DOCUMENT:
- personsList = new ArrayList<Person>(); // 初始化persons
- xmlHeader = new StringBuffer(); // 初始化xmlHeader
- 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(); // 创建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) { // XmlPullParserFactory.newInstance
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return xmlWriter.toString();
- }
运行结果: