e523. generating sax parsing events by traversing a dom document.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 34 行

TXT
34
字号
If you have developed a set of handlers for SAX events, it is possible to use these handlers on a source other than a file. This example demonstrates how to use the handlers on a DOM document. 
    // Obtain a DOM document; this method is implemented in
    // e510 The Quintessential Program to Create a DOM Document from an XML File
    Document doc = parseXmlFile("infilename.xml", false);
    
    // Prepare the DOM source
    Source source = new DOMSource(doc);
    
    // Set the systemId of the source. This call is not strictly necessary
    URI uri = new File("infilename.xml").toURI();
    source.setSystemId(uri.toString());
    
    // Create a handler to handle the SAX events
    DefaultHandler handler = new MyHandler();
    
    try {
        // Prepare the result
        SAXResult result = new SAXResult(handler);
    
        // Create a transformer
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
    
        // Traverse the DOM tree
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
    
    // DefaultHandler contain no-op implementations for all SAX events.
    // This class should override methods to capture the events of interest.
    class MyHandler extends DefaultHandler {
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?