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);

    }

  • 相关阅读:
    8.ffmpeg-基础常用知识
    7.SwrContext音频重采样使用
    6.AVCodecContext和AVCodec
    5.AVStream和AVCodecParameters
    Discuz论坛禁止匿名发贴,却出现匿名发帖或回复?找不到发帖用户,DZ如何禁止匿名发帖修改教程
    在独立服务器上通过Ubuntu 18.04+apache2+php5.6+mysql5.7+discuz!x3.4搭建的论坛实现伪静态的正确方法
    [源码] 2200套微信小程序源码
    discuz论坛更换目录后出现头像无法显示/ucenter无法进入
    discuz 您的服务器不支持 CURL,这将会导致应用无法安装。请联系您的服务商或者网站技术人员
    Discuz在服务器配置安装时出现xml_parser_create()不支持
  • 原文地址:https://www.cnblogs.com/wangfeng520/p/5068818.html
Copyright © 2011-2022 走看看