关闭

pull和sax解析xml文件

148人阅读 评论(0) 收藏 举报
分类:
/***
     * 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);
            }
        }
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:70486次
    • 积分:1617
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:34篇
    • 译文:0篇
    • 评论:6条
    最新评论