甜咖啡

          我的IT空間

          SAX解析XML

          package com;
            
            import java.io.FileInputStream;
            import java.io.InputStream;
            import java.util.ArrayList;
            import java.util.List;
            
            import javax.xml.parsers.SAXParser;
            import javax.xml.parsers.SAXParserFactory;
            
            import org.xml.sax.Attributes;
            import org.xml.sax.SAXException;
            import org.xml.sax.helpers.DefaultHandler;
            
            /**
             * SAX解析XML,事件驅動
             * 只有兩種節點
             * Element Node元素節點
             * Text Node文本節點 
             */
            public class SaxResolveXML {
           
            public static void main(String[] args){
            try {
            SaxResolveXML saxResolveXML = new SaxResolveXML();
            InputStream inStream = new FileInputStream("D:\\xml.xml");
            List<Person> list = saxResolveXML.getList(inStream);
            for(Person person : list){
            System.out.println(person.toString());
            }
            } catch (Exception e) {
            e.printStackTrace();
            }
           
            }
           
            public List<Person> getList(InputStream inStream) throws Exception {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parse = factory.newSAXParser();
            SaxResolve saxResolve = new SaxResolve();
            parse.parse(inStream, saxResolve);
            inStream.close();
            return saxResolve.getPerson();
            }
           
            private final class SaxResolve extends DefaultHandler {
           
            private List<Person> list = null;
            private Person person = null;
            private String tag = null;
           
            public List<Person> getPerson(){
            return list;
            }
           
            //開始文檔事件
            public void startDocument() throws SAXException {
            //初始化
            list = new ArrayList<Person>();
            }
            
            //開始元素語法事件  參數說明:命名空間、不帶命名空間的標簽名、含有命名空間前綴的標簽名、屬性
            public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {
            if("person".equals(qName)){
            person = new Person();
            person.setId(Integer.parseInt(atts.getValue(0)));
            }
            tag = qName;
            }
           
            //觸發文本節點事件  參數說明:整個xml內容的字符串、當前讀到文本類型的開始位置、當前讀到文本的數據長度
            public void characters(char[] ch, int start, int length)
            throws SAXException {
            if(tag != null){
            String data = new String(ch, start, length);
            if(tag.equals("name")){
            person.setName(data);
            }else if(tag.equals("age")){
            person.setAge(Integer.parseInt(data));
            }
            }
            }
           
            //結束元素語法事件  參數說明:命名空間、不帶命名空間的標簽名、含有命名空間前綴的標簽名
            public void endElement(String uri, String localName, String qName)
            throws SAXException {
            if("person".equals(qName)){
            list.add(person);
            person = null;
            //對象設為空
            }
            tag = null;
            }
            }
           
           
            /*//開始文檔事件
            public void startDocument() throws SAXException {
           
            }
            
            //開始元素語法事件  參數說明:命名空間、不帶命名空間的標簽名、含有命名空間前綴的標簽名、屬性
            public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {
           
            }
           
            //觸發文本節點事件  參數說明:整個xml內容的字符串、當前讀到文本類型的開始位置、當前讀到文本的數據長度
            public void characters(char[] ch, int start, int length)
            throws SAXException {
           
            }
           
            //結束元素語法事件  參數說明:命名空間、不帶命名空間的標簽名、含有命名空間前綴的標簽名
            public void endElement(String uri, String localName, String qName)
            throws SAXException {
           
            }
            
            public void endDocument() throws SAXException {
           
            }
            
            public void endPrefixMapping(String prefix) throws SAXException {
           
            }
            
            public void ignorableWhitespace(char[] ch, int start, int length)
            throws SAXException {
           
            }
            
            public void processingInstruction(String target, String data)
            throws SAXException {
           
            }
            
            public void setDocumentLocator(Locator locator) {
           
            }
            
            public void skippedEntity(String name) throws SAXException {
           
            }
            
            public void startPrefixMapping(String prefix, String uri)
            throws SAXException {
           
            }*/
           
            }
            
           
           
           
           xml文件如下:
           <?xml version="1.0" encoding="UTF-8"?>
            <persons>
                <person id="1">
                    <name>liming</name>
                    <age>23</age>
                </person>
                <person id="2">
                    <name>lixiangmei</name>
                    <age>24</age>
                </person>
            </persons>
            
           
           
           

          posted on 2012-12-25 16:52 甜咖啡 閱讀(381) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2012年12月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          統計

          常用鏈接

          留言簿(1)

          我參與的團隊

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 类乌齐县| 乌海市| 夹江县| 静安区| 仙桃市| 景东| 乃东县| 青冈县| 崇礼县| 微山县| 贺州市| 城口县| 历史| 土默特左旗| 收藏| 定远县| 兴安县| 青海省| 二手房| 手机| 吴堡县| 毕节市| 江津市| 蚌埠市| 凌源市| 叶城县| 大埔县| 乡宁县| 濮阳县| 文成县| 湘西| 洪江市| 方正县| 黔西县| 涞源县| 讷河市| 泗阳县| 佛山市| 马关县| 宾川县| 高雄市|