用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

解析XML

2016-12-16 作者: cwm1996举报

[android]代码库

SAX解析:
// 得到工厂类
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        // 得到解析类
        try {
            SAXParser saxParser = saxParserFactory.newSAXParser();
            // 得到读取类
            XMLReader xmlReader = saxParser.getXMLReader();
            // 设置内容处理者
            xmlReader.setContentHandler(new DefaultHandler() {             
                // 读取文本
 
                // 读取到结束标签
                @Override
                public void endElement(String uri, String localName,
                        String qName) throws SAXException {
                    if (localName.equals("student")) {
                        list.add(student);                     
                    }
                    tagName = "";
                    super.endElement(uri, localName, qName);
                }
                @Override
                public void characters(char[] ch, int start, int length)
                        throws SAXException {
                    String tag = new String(ch);
                    if ("name".equals(tagName)) {
                        Log.i("ASD", "开始解析文件"+tag);
                        student.setName(tag.trim());
                    } else if ("sex".equals(tagName)) {
                        student.setSex(tag.trim());
                    } else if ("address".equals(tagName)) {
                        student.setAddress(tag.trim());
                    } else if ("hobby".equals(tagName)) {
                        student.setHobby(tag.trim());
                    }
                     
                    super.characters(ch, start, length);
                }
 
                // 文档读取完毕
                @Override
                public void endDocument() throws SAXException {
                    Log.i("ASD", "文件解析完毕");
                    super.endDocument();
                }
 
                // 开始读取文档
                @Override
                public void startDocument() throws SAXException {
                    Log.i("ASD", "开始解析文件");
                    super.startDocument();
                }
 
                // 读取到开始标签
                @Override
                public void startElement(String uri, String localName,
                        String qName, Attributes attributes)
                        throws SAXException {
                    tagName = localName;
                    if (tagName.equals("student")) {
                        Log.i("ASD", "开始解析文件"+tagName);
                        student = new Student();
                        String id = attributes.getValue("id");
                        String age = attributes.getValue("age");
                        student.setAge(age.trim());
                    }
                    super.startElement(uri, localName, qName, attributes);
                }
 
            });
            xmlReader.parse(new InputSource(getAssets().open("student.xml")));
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
 
PULL解析:
 
// 获取XML文件
            InputStream inputStream = getAssets().open("student.xml");
            pullParser = Xml.newPullParser();
            // 给解析对象设置输入流
            pullParser.setInput(inputStream, "utf_8");
            // 获取xml的标签类型
            int type = pullParser.getEventType();
            // 当没有获取到结束标签时循环执行
            while (type != XmlPullParser.END_DOCUMENT) {
                switch (type) {
                // 开始标签
                case XmlPullParser.START_TAG:
                    // 获取开始标签名称
                    name = pullParser.getName();
                    if (name.equals("student")) {
                        student = new Student();
                        // 获取开始标签的属性
                        //String id = pullParser.getAttributeValue(0);
                        String age = pullParser.getAttributeValue(1);
                        student.setAge(age);
                    } else if (name.equals("name")) {
                        String name = pullParser.nextText();
                        student.setName(name);
                    } else if (name.equals("sex")) {
                        String sex = pullParser.nextText();
                        student.setSex(sex);
                    } else if (name.equals("address")) {
                        String address = pullParser.nextText();
                        student.setAddress(address);
                    } else if (name.equals("hobby")) {
                        String hobby = pullParser.nextText();
                        student.setHobby(hobby);
                    }
                    break;
                // 结束标签
                case XmlPullParser.END_TAG:
                    name = pullParser.getName();
                    if ("student".equals(name)) {
                        // 将当前对象添加到集合
                        list.add(student);
                        student = null;
                    }
                    break;
                }
 
                type = pullParser.next();
            }
             
        } catch (IOException e) {
            // TODO Auto-generated catch block
             
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...