当前位置:  开发笔记 > 编程语言 > 正文

Java 6中对xinclude的默认支持?

如何解决《Java6中对xinclude的默认支持?》经验,为你挑选了1个好方法。

我在我的应用程序中看到,在我的解析XML文件中包含xinclude在我的Java XSLT转换中不起作用.

但是,虽然我这样做:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setXIncludeAware(true);

我不是专门设置变压器工厂为System.getProperty("javax.xml.transform.TransformerFactory")返回"null".

我的问题:默认的Java(1.6或6)是否支持xinclude,还是我必须添加替代的XSLT解析器,例如Apache Xerces?



1> McDowell..:

根据规范,自Java 1.5(5)以来一直支持.我相信XInclude支持依赖于名称空间感知,默认情况下会出于向后兼容性原因而关闭.

public class XIncludeDemo {

    private static final String XML = "\n"
            + "\n"
            + "\n"
            + "\n";

    private static final String INCLUDE = "Hello, World!";

    public static void main(String[] args) throws Exception {
        // data
        final InputStream xmlStream = new ByteArrayInputStream(XML
                .getBytes("UTF-8"));
        final InputStream includeStream = new ByteArrayInputStream(INCLUDE
                .getBytes("UTF-8"));
        // document parser
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setXIncludeAware(true);
        factory.setNamespaceAware(true);
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        if (!docBuilder.isXIncludeAware()) {
            throw new IllegalStateException();
        }
        docBuilder.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                if (systemId.endsWith("include.txt")) {
                    return new InputSource(includeStream);
                }
                return null;
            }
        });
        Document doc = docBuilder.parse(xmlStream);
        // print result
        Source source = new DOMSource(doc);
        Result result = new StreamResult(System.out);
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(source, result);
    }

}

推荐阅读
无名有名我无名_593
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有