e522. transforming an xml file with xsl into a dom document.txt

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

TXT
39 行
字号
// This method applies the xslFilename to inFilename and
    // returns DOM document containing the result.
    public static Document parseXmlFile(String inFilename, String xslFilename) {
        try {
            // Create transformer factory
            TransformerFactory factory = TransformerFactory.newInstance();
    
            // Use the factory to create a template containing the xsl file
            Templates template = factory.newTemplates(new StreamSource(
                new FileInputStream(xslFilename)));
    
            // Use the template to create a transformer
            Transformer xformer = template.newTransformer();
    
            // Prepare the input file
            Source source = new StreamSource(new FileInputStream(inFilename));
    
            // Create a new document to hold the results
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.newDocument();
            Result result = new DOMResult(doc);
    
            // Apply the xsl file to the source file and create the DOM tree
            xformer.transform(source, result);
            return doc;
        } catch (ParserConfigurationException e) {
            // An error occurred while creating an empty DOM document
        } catch (FileNotFoundException e) {
        } catch (TransformerConfigurationException e) {
            // An error occurred in the XSL file
        } catch (TransformerException e) {
            // An error occurred while applying the XSL file
        }
        return null;
    }

 Related Ex 

⌨️ 快捷键说明

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