• Java-->xml的pull解析


    --> pull解析器是android内置的解析器,解析原理与sax类似

    --> xml文件student.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!-- 有三个学生:姓名、年龄、性别。把这三个学生的信息用xml表示出来 -->
     3 <students>
     4     <student name="zhangsan">
     5         <age>18</age>
     6         <sex></sex>
     7     </student>
     8     <student name="lisi">
     9         <age>22</age>
    10         <sex></sex>
    11     </student>
    12     <student name="wangwu">
    13         <age>20</age>
    14         <sex></sex>
    15     </student>
    16 </students>

    --> Student 类:

     1 package com.dragon.java.pullparse;
     2 
     3 public class Student {
     4     private String name;
     5     private int age;
     6     private String sex;
     7 
     8     public Student() {
     9         super();
    10     }
    11 
    12     public Student(String name, int age, String sex) {
    13         super();
    14         this.name = name;
    15         this.age = age;
    16         this.sex = sex;
    17     }
    18 
    19     public String getName() {
    20         return name;
    21     }
    22 
    23     public void setName(String name) {
    24         this.name = name;
    25     }
    26 
    27     public int getAge() {
    28         return age;
    29     }
    30 
    31     public void setAge(int age) {
    32         this.age = age;
    33     }
    34 
    35     public String getSex() {
    36         return sex;
    37     }
    38 
    39     public void setSex(String sex) {
    40         this.sex = sex;
    41     }
    42 
    43     @Override
    44     public String toString() {
    45         return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    46     }
    47 
    48 }
    Student

    --> Test 测试类:

     1 package com.dragon.java.pullparse;
     2 
     3 import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
     4 import static org.xmlpull.v1.XmlPullParser.END_TAG;
     5 import static org.xmlpull.v1.XmlPullParser.START_TAG;
     6 import static org.xmlpull.v1.XmlPullParser.TEXT;
     7 
     8 import java.io.FileReader;
     9 import java.io.IOException;
    10 import java.util.ArrayList;
    11 import java.util.List;
    12 
    13 import org.xmlpull.v1.XmlPullParser;
    14 import org.xmlpull.v1.XmlPullParserException;
    15 import org.xmlpull.v1.XmlPullParserFactory;
    16 
    17 /*
    18  * pull解析器
    19  */
    20 public class Test {
    21     public static void main(String[] args) throws XmlPullParserException,
    22             IOException {
    23         List<Student> list = null;
    24         Student student = null;
    25         // 1、获取一个XmlPullParser解析器
    26         XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); // 调用解析器工厂的静态方法,创建一个解析器工厂
    27         XmlPullParser parser = factory.newPullParser(); // 创建一个pull解析器
    28         parser.setInput(new FileReader(
    29                 "D:/workspace/08-25/src/com/dragon/java/xml/students.xml")); // 设置一个输入流,告诉要解析的xml数据的来源
    30         int event = parser.getEventType(); // 获取当前事件的类型
    31         while (event != END_DOCUMENT) { // 当碰到文档结束的时候,结束循环
    32             switch (event) {
    33             case START_TAG:
    34                 String tag = parser.getName();
    35                 if (tag.equals("students")) {
    36                     list = new ArrayList<>();
    37                 } else if (tag.equals("student")) {
    38                     student = new Student();
    39                     student.setName(parser.getAttributeValue(null, "name")); // 参数1:是命名空间,如果命名空间,则传入null。参数2:属性的名字
    40                     list.add(student);
    41                 } else if (tag.equals("age")) {
    42                     student.setAge(Integer.parseInt(parser.nextText())); // 获取当前事件后面的text事件,text的值
    43                 } else if (tag.equals("sex")) {
    44                     student.setSex(parser.nextText());
    45                 }
    46                 break;
    47             case END_TAG:
    48                 break;
    49             case TEXT:
    50                 break;
    51             default:
    52                 break;
    53             }
    54             event = parser.next();
    55         }
    56         for (Student student2 : list) {
    57             System.out.println(student2);
    58         }
    59     }
    60 }

    --> 也是蛮麻烦的...

  • 相关阅读:
    ios开发中的基本设计模式
    Swift进阶
    转帖:深入理解JavaScript系列
    触摸事件、手势识别、摇晃事件、耳机线控
    打造自己的“美图秀秀”
    地图与定位
    python 深拷贝,浅拷贝
    Django 路由(SimpleRouter, DefaultRouter,ExtendedDefaultRouter)
    Django 国际化(翻译)
    wkhtmltopdf 转换后 表格不显示 python解决方案
  • 原文地址:https://www.cnblogs.com/xmcx1995/p/5806409.html
Copyright © 2020-2023  润新知