zoukankan      html  css  js  c++  java
  • XML解析

    // xml,资源解析器,解析本地文件
    XmlResourceParser parser = getResources().getXml(R.xml.customer);
    try {
    int type = parser.getEventType();// 解析到的类型
    while (type != XmlResourceParser.END_DOCUMENT) {// 没有解析到结尾,有些地方没有内容为空
    if (type == XmlResourceParser.START_TAG) {// 标签开始的地方<
    String tagname = parser.getName();// 标签名
    if (tagname.equals("student")) {
    student = new Student();
    student.setId(parser.getAttributeValue(0));
    student.setName(parser.getAttributeValue(1));
    student.setAge(Integer.parseInt(parser
    .getAttributeValue(2)));
    students.add(student);
    }else if (tagname.equals("teacher")) {
    teacher = new Teacher();
    teacher.setId(parser.getAttributeValue(0));
    teacher.setName(parser.getAttributeValue(1));
    teacher.setAge(Integer.parseInt(parser
    .getAttributeValue(2)));
    teachers.add(teacher);
    }else if (tagname.equals("customer")) {
    customer = new Customer();
    customer.setId(parser.getAttributeValue(0));
    customer.setName(parser.getAttributeValue(1));
    customer.setAge(Integer.parseInt(parser
    .getAttributeValue(2)));
    customers.add(customer);
    }
    }
    type = parser.next();// 继续解析
    }
    } catch (XmlPullParserException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    // 自定义一个pull解析的方法
    public void parseXmlWithPull(InputStream is) {
    // 将Xml流对象解析出来

    try {// 实例化一个解析工厂
    XmlPullParserFactory parserFactory = XmlPullParserFactory
    .newInstance();
    // 通过解析工厂得到pull解析器
    XmlPullParser pullParser = parserFactory.newPullParser();
    // 将连接得到的流媒体进行解析并设置编码类型
    pullParser.setInput(is, "utf-8");
    // 开始解析
    int eventType = pullParser.getEventType();// 解析每一个<>标签,是什么类型(文档结束,开始,标签开始,结束或者null)
    while (eventType != XmlPullParser.END_DOCUMENT) {// 不是文档结尾
    if (eventType == XmlPullParser.START_TAG) {
    String tagname = pullParser.getName();// 标签名
    if (tagname.equals("student")) {
    student = new Student();
    student.setId(pullParser.getAttributeValue(0));
    student.setName(pullParser.getAttributeValue(1));
    student.setAge(Integer.parseInt(pullParser
    .getAttributeValue(2)));
    students.add(student);
    }
    if (tagname.equals("teacher")) {
    teacher = new Teacher();
    teacher.setId(pullParser.getAttributeValue(0));
    teacher.setName(pullParser.getAttributeValue(1));
    teacher.setAge(Integer.parseInt(pullParser
    .getAttributeValue(2)));
    teachers.add(teacher);
    }
    if (tagname.equals("customer")) {
    customer = new Customer();
    customer.setId(pullParser.getAttributeValue(0));
    customer.setName(pullParser.getAttributeValue(1));
    customer.setAge(Integer.parseInt(pullParser
    .getAttributeValue(2)));
    customers.add(customer);
    }
    }
    eventType = pullParser.next();// 继续解析下一个,不让它死循环下去
    }
    } catch (XmlPullParserException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    handler.sendEmptyMessage(1);

    }

  • 相关阅读:
    概率论——随机事件及其概率
    Web应用程序项目以配置使用IIS。未找到Web服务器”链接地址”
    LaTeX中.sty文件缺失解决办法
    IIS中的经典模式和集成模式有什么区别
    判断有序整型数组中是否存在两数,相加之和等于给定的任意整数
    51Job的搜索技巧
    登录失败。该登录名来自不受信任的域,不能与 Windows 身份验证一起使用。
    Ubuntu使用Latex模板moderncv写简历
    COM相关操作(C#)
    什么是单线程单元(STA)什么是多线程单元(MTA)
  • 原文地址:https://www.cnblogs.com/wangfeng520/p/5068818.html
Copyright © 2011-2022 走看看