zoukankan      html  css  js  c++  java
  • Android数据篇SAX解析XML

    作为三大解析XML方式之一,这里介绍的是SAX解析:

    第一步:

            try {
                //创建一个解析器的工厂
                SAXParserFactory factory = SAXParserFactory.newInstance();
                XMLReader xmlReader = factory.newSAXParser().getXMLReader();
                //为XMLReader设置内容处理器
                xmlReader.setContentHandler(new MyContentHandler());
                xmlReader.parse(new InputSource(new StringReader(str)));
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

    第二步:

    public class MyContentHandler extends DefaultHandler{
        private String tagName;//标示 
        private String hisName,address,sex,status,money;
        
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            // TODO Auto-generated method stub
            if(tagName.equals("name")){
                hisName = new String(ch,start,length);
            }else if(tagName.equals("sex")){
                sex = new String(ch, start, length);
            }else if(tagName.equals("status")){
                status = new String(ch, start, length);
            }else if(tagName.equals("address")){
                address = new String(ch, start, length);
            }else if(tagName.equals("money")){
                money = new String(ch, start, length);
            }
        }
    
        @Override
        public void endDocument() throws SAXException {
            // TODO Auto-generated method stub
            Log.i("+++","end");
            super.endDocument();
        }
    
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            // TODO Auto-generated method stub
            if(localName.equals("worker")){
                //解析结束 打印出相关字段
                printOut();
            }
            super.endElement(uri, localName, qName);
        }
    
        @Override
        public void startDocument() throws SAXException {
            // TODO Auto-generated method stub
            Log.i("+++", "start");
            super.startDocument();
        }
        
        /**
         * @param uri 当前标签的命名空间
         * @param localName 不带前缀
         * @param qName 带前缀 
         * @param attributes 标签的所有属性
         */
        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            // TODO Auto-generated method stub
            tagName = localName;
            if(localName.equals("worker")){
                for(int i=0;i<attributes.getLength();i++){
                    System.out.print(attributes.getLocalName(i)+"="+attributes.getValue(i));
                }
            }
            super.startElement(uri, localName, qName, attributes);
        }
        
        private void printOut(){
            Log.i("json", "name: " + hisName);
            Log.i("json", "sex= " + sex);
            Log.i("json", "address: " + address);
            Log.i("json", "status= " + status);
            Log.i("json", "money= " + money);
        }
  • 相关阅读:
    2016百度之星资格赛 Problem B(大数+组合数)
    HDU 4380 Farmer Greedy(叉积和三角形知识的综合应用)
    C++ STL (备忘)
    【Linked List Cycle II】cpp
    【Linked List Cycle】cpp
    【Copy List with Random Pointer】cpp
    【Reverse Nodes in k-Group】cpp
    【Swap Nodes in Pairs】cpp
    【Remove Nth Node From End of List】cpp
    【Rotate List】cpp
  • 原文地址:https://www.cnblogs.com/gongcb/p/2507008.html
Copyright © 2011-2022 走看看