pull和sax解析xml文件
/***
     * pull解析
     **/
    public void parseXMLWithPull(String xmlData) {
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            parser.setInput(new StringReader(xmlData));
            int evenType = parser.getEventType();
            String id = "";
            String name = "";
            String version = "";
            while (evenType != XmlPullParser.END_DOCUMENT) {
                String nodeName = parser.getName();
                switch (evenType) {
                    //开始解析某个结点
                    case XmlPullParser.START_TAG:
                        if ("id".equals(nodeName)) {
                            id = parser.nextText();
                        } else if ("name".equals(nodeName)) {
                            name = parser.nextText();
                        } else if ("version".equals(nodeName)) {
                            version = parser.nextText();
                        }
                        break;
                    //完成解析某个结点
                    case XmlPullParser.END_TAG:
                        if ("app".equals(nodeName)) {
                            Log.i("xmlParseActivity", "id is " + id);
                            Log.i("xmlParseActivity", "name is " + name);
                            Log.i("xmlParseActivity", "version is " + version);
                        }
                        break;
                    default:
                        break;
                }
                evenType = parser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * SAX解析
     ***/
    public void parseXmLWithSAX(String xmlData) {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            XMLReader reader = factory.newSAXParser().getXMLReader();
            //将ContentHandler的实例设置到XMLReader中
            ContentHandler handler = new ContentHandler();
            reader.setContentHandler(handler);
            //开始执行解析
            reader.parse(new InputSource(new StringReader(xmlData)));
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * SAX解析调用的方法
     ***/
    public class ContentHandler extends DefaultHandler {
        private String nodeName;
        private StringBuilder id;
        private StringBuilder name;
        private StringBuilder version;

        public ContentHandler() {
            super();
        }

        @Override
        public void startDocument() throws SAXException {
            super.startDocument();
            id = new StringBuilder();
            name = new StringBuilder();
            version = new StringBuilder();
        }

        @Override
        public void endDocument() throws SAXException {
            super.endDocument();
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            super.startElement(uri, localName, qName, attributes);
            //记录当前结点名
            nodeName = localName;
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            super.endElement(uri, localName, qName);
            if ("app".equals(localName)) {
                Log.i("contentHandler", "id is " + id.toString().trim());
                Log.i("contentHandler", "name is " + name.toString().trim());
                Log.i("contentHandler", "version is " + version.toString().trim());
                //最后将StringBuilder清空掉
                id.setLength(0);
                name.setLength(0);
                version.setLength(0);
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            super.characters(ch, start, length);
            //根据当前的结点名判断将内动添加到哪一个StringBuilder对象中
            if ("id".equals(nodeName)) {
                id.append(ch, start, length);
            } else if ("name".equals(nodeName)) {
                name.append(ch, start, length);
            } else if ("version".equals(nodeName)) {
                version.append(ch, start, length);
            }
        }
    }
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/uniquemei/article/details/52383459
个人分类: Android进阶
想对作者说点什么? 我来说一句

Android .xml文件三种解析方式

2014年07月30日 1.48MB 下载

Java解析XML

2016年08月20日 53KB 下载

解析xml文件的三种方式

2015年09月06日 1.05MB 下载

DOM SAXPULL解析

DOM SAX pull

qq_33344834 qq_33344834

2016-07-09 11:11:17

阅读数:368

没有更多推荐了,返回首页

不良信息举报

pull和sax解析xml文件

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭