<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    數(shù)據(jù)加載中……
    JAVA XML 解析之四 -SAX
    2008年4月21日  Edited By DingDangXiaoMa
    SAX 是采用事件驅(qū)動的方式與Dom最大的不同是,它不用讀取完所有的XML文件就可以進行處理。采用流式的處理方式(呵,呵,我也不知道是什么意思)
    example:
    注冊內(nèi)容管理器:MyContentHandler.java
    public class MyContentHandler implements ContentHandler {
        
    // DTD中定義的元素
        private static final String ELEMENT_NAME = "name";
        
    private static final String ELEMENT_SEX = "sex";
        
    private static final String ELEMENT_LESSON = "lesson";
        
    private static final String ELEMENT_LESSON_NAME = "lessonName";
        
    private static final String ELEMENT_LESSON_SCORE = "lessonScore";
        
    private static final String ELEMENT_STUDENT = "student";
        
    private static final String ELEMENT_LINE = "breakLine";

        
    private String currentData = ""// 當(dāng)前元素的數(shù)據(jù)
        private String lessonName = "";
        
    private String lessonScore = "";

        
    public MyContentHandler() {
        }

        
    /**
         * 當(dāng)其他某一個調(diào)用事件發(fā)生時,先調(diào)用此方法來在文檔中定位。
         * 
         * 
    @param locator
         
    */
        
    public void setDocumentLocator(Locator locator) {

        }

        
    /**
         * 在解析整個文檔開始時調(diào)用
         * 
         * 
    @throws SAXException
         
    */
        
    public void startDocument() throws SAXException {
            System.out.println(
    "**** Student information start ****");
        }

        
    /**
         * 在解析整個文檔結(jié)束時調(diào)用
         * 
         * 
    @throws SAXException
         
    */
        
    public void endDocument() throws SAXException {
            System.out.println(
    "**** Student information end ****");
        }

        
    /**
         * 在解析名字空間開始時調(diào)用
         * 
         * 
    @param prefix
         * 
    @param uri
         * 
    @throws SAXException
         
    */
        
    public void startPrefixMapping(String prefix, String uri)
                
    throws SAXException {

        }

        
    /**
         * 在解析名字空間結(jié)束時調(diào)用
         * 
         * 
    @param prefix
         * 
    @throws SAXException
         
    */
        
    public void endPrefixMapping(String prefix) throws SAXException {

        }

        
    /**
         * 在解析元素開始時調(diào)用
         * 
         * 
    @param namespaceURI
         * 
    @param localName
         * 
    @param qName
         * 
    @param atts
         * 
    @throws SAXException
         
    */
        
    public void startElement(String namespaceURI, String localName,
                String qName, Attributes atts) 
    throws SAXException {

        }

        
    /**
         * 在解析元素結(jié)束時調(diào)用
         * 
         * 
    @param namespaceURI
         * 
    @param localName
         *            本地名,如student
         * 
    @param qName
         *            原始名,如LIT:student
         * 
    @throws SAXException
         
    */
        
    public void endElement(String namespaceURI, String localName, String qName)
                
    throws SAXException {
            
    if (localName.equals(ELEMENT_NAME)) {
                System.out.println(localName 
    + ":" + currentData);
            }

            
    if (localName.equals(ELEMENT_SEX)) {
                System.out.println(localName 
    + ":" + currentData);
            }

            
    if (localName.equals(ELEMENT_LESSON_NAME)) {
                
    this.lessonName = currentData;
            }

            
    if (localName.equals(ELEMENT_LESSON_SCORE)) {
                
    this.lessonScore = currentData;
            }

            
    if (localName.equals(ELEMENT_LESSON)) {
                System.out.println(lessonName 
    + ":" + lessonScore);
            }

            
    if (localName.equals(ELEMENT_LINE)) {
                System.out.println(
    "------------------------------------");
            }
        }

        
    /**
         * 取得元素數(shù)據(jù)
         * 
         * 
    @param ch
         * 
    @param start
         * 
    @param length
         * 
    @throws SAXException
         
    */
        
    public void characters(char[] ch, int start, int length)
                
    throws SAXException {
            currentData 
    = new String(ch, start, length).trim();
        }

        
    /**
         * 取得元素數(shù)據(jù)中的空白
         * 
         * 
    @param ch
         * 
    @param start
         * 
    @param length
         * 
    @throws SAXException
         
    */
        
    public void ignorableWhitespace(char[] ch, int start, int length)
                
    throws SAXException {

        }

        
    /**
         * 在解析到處理指令時,調(diào)用此方法。 這些處理指令不包括XML的版權(quán)指令,它由解析器本身識別。
         * 
         * 
    @param target
         * 
    @param data
         * 
    @throws SAXException
         
    */
        
    public void processingInstruction(String target, String data)
                
    throws SAXException {

        }

        
    /**
         * 當(dāng)未驗證解析器忽略實體時調(diào)用此方法
         * 
         * 
    @param name
         * 
    @throws SAXException
         
    */
        
    public void skippedEntity(String name) throws SAXException {

        }
    錯誤管理器:MyErrorHandler.java
    public class MyErrorHandler implements ErrorHandler {

        
    public MyErrorHandler() {
        }

        
    /**
         * XML的警告信息
         * 
         * 
    @param exception
         * 
    @throws SAXException
         
    */
        
    public void warning(SAXParseException exception) throws SAXException {
            System.out.println(
    "!!!WARNING!!!");
            System.out.println(exception.getLineNumber() 
    + ":("
                    
    + exception.getSystemId() + ")" + exception.getMessage());
        }

        
    /**
         * 不符合XML規(guī)范時調(diào)用此方法
         * 
         * 
    @param exception
         * 
    @throws SAXException
         
    */
        
    public void error(SAXParseException exception) throws SAXException {
            System.out.println(
    "!!!ERROR!!!");
            System.out.println(exception.getLineNumber() 
    + ":("
                    
    + exception.getSystemId() + ")" + exception.getMessage());
        }

        
    /**
         * 非良構(gòu)的文檔時調(diào)用此方法
         * 
         * 
    @param exception
         * 
    @throws SAXException
         
    */
        
    public void fatalError(SAXParseException exception) throws SAXException {
            System.out.println(
    "!!!FATAL!!!");
            System.out.println(exception.getLineNumber() 
    + ":("
                    
    + exception.getSystemId() + ")" + exception.getMessage());
        }
    }
    DTD 管理器: MyDTDHandler.java
    public class MyDTDHandler implements DTDHandler {

        
    public MyDTDHandler() {
        }

        
    /**
         * 當(dāng)實體聲明為不必解析的實體時調(diào)用此方法,比如NDATA類型。
         * 
         * 
    @param name
         * 
    @param publicId
         * 
    @param systemId
         * 
    @throws SAXException
         
    */
        
    public void notationDecl(String name, String publicId, String systemId)
                
    throws SAXException {
            System.out.println(
    "**notationDecl**");
            System.out.println(
    "name:" + name);
            System.out.println(
    "publicId" + publicId);
            System.out.println(
    "systemId:" + systemId);
        }

        
    /**
         * 當(dāng)處理符號聲明時調(diào)用此方法,比如NOTATION。
         * 
         * 
    @param name
         * 
    @param publicId
         * 
    @param systemId
         * 
    @param notationName
         * 
    @throws SAXException
         
    */
        
    public void unparsedEntityDecl(String name, String publicId,
                String systemId, String notationName) 
    throws SAXException {
            System.out.println(
    "**unparsedEntityDecl**");
            System.out.println(
    "name:" + name);
            System.out.println(
    "publicId" + publicId);
            System.out.println(
    "systemId:" + systemId);
            System.out.println(
    "notationName:" + notationName);
        }
    }
    SAX方式處理:MySAXParser.java
    import java.io.IOException;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.XMLReaderFactory;
    public class MySAXParser {
        
    public MySAXParser() {
        }
        
    public static void main(String[] args) {
            MySAXParser mySAXParser 
    = new MySAXParser();
            mySAXParser.parserXMLFile(
    "http://localhost/struts2.0/xml/SutInfo.xml");
        }
        
    /**
         * 解析文檔XML文檔的URI
         * 
    @param fileURI
         
    */
        
    private void parserXMLFile(String fileURI) {
            
    try {
                
    // 通過指定解析器的名稱來動態(tài)加載解析器
                XMLReader parser = XMLReaderFactory
                        .createXMLReader(
    "org.apache.xerces.parsers.SAXParser");
                
    // 處理內(nèi)容前要注冊內(nèi)容管理器
                parser.setContentHandler(new MyContentHandler());
                
    // 處理錯誤前要注冊錯誤管理器
                parser.setErrorHandler(new MyErrorHandler());
                
    // 處理DTD前要注冊DTD管理器
                parser.setDTDHandler(new MyDTDHandler());
                
    // 打開解析器的驗證
                parser.setFeature("http://xml.org/sax/features/validation"true);
                
    // 開始解析文檔
                parser.parse(fileURI);
            } 
    catch (IOException ioe) {
                System.out.println(ioe.getMessage());
            } 
    catch (SAXException saxe) {
                System.out.println(saxe.getMessage());
            }
        }
    }
    說明:
    org.xml.sax.*,包含于jdk 中。
    這個例子是 javaresearch 上的例子,我也沒有弄明白是什么意思,呵。呵。誰若是看到了些貼,請回復(fù),說一說您的看法。

    posted on 2008-04-21 17:34 叮當(dāng)小馬 閱讀(188) 評論(0)  編輯  收藏 所屬分類: XML

    主站蜘蛛池模板: 4444www免费看| 国产乱弄免费视频| 亚洲精品中文字幕无码A片老| 成人免费午夜视频| 免费人成网上在线观看| 亚洲不卡中文字幕无码| 人禽杂交18禁网站免费| 又大又硬又粗又黄的视频免费看| 亚洲AV日韩AV永久无码下载| 手机在线免费视频| 在线观看免费黄色网址| 亚洲精品中文字幕无乱码麻豆| 亚洲国产电影av在线网址| 69视频免费在线观看| 看一级毛片免费观看视频| 99亚洲精品高清一二区| 亚洲成人影院在线观看| 4399影视免费观看高清直播| 亚美影视免费在线观看| 狠狠色伊人亚洲综合网站色| 亚洲gv猛男gv无码男同短文| 情侣视频精品免费的国产| 小日子的在线观看免费| 深夜久久AAAAA级毛片免费看| 亚洲中文字幕久久精品无码2021| JLZZJLZZ亚洲乱熟无码| 好爽又高潮了毛片免费下载| 久久久久久久99精品免费| 青娱乐在线免费观看视频| 亚洲一级毛片免费看| 亚洲AV电影院在线观看| 久久久久亚洲AV成人网人人网站 | 亚洲男人在线无码视频| 最近免费中文字幕4| 8888四色奇米在线观看免费看| 五级黄18以上免费看| 久久久久亚洲国产AV麻豆| 亚洲国产精品成人综合久久久| 自拍偷自拍亚洲精品情侣| 国产zzjjzzjj视频全免费| 中文字幕无码成人免费视频|