gpt4 book ai didi

java - Java 6 中对 xinclude 的默认支持?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:39:37 26 4
gpt4 key购买 nike

我在我的应用程序中看到,我解析的 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?

最佳答案

根据 the spec ,自 Java 1.5 (5) 以来一直支持。我相信 XInclude 支持依赖于 namespace awareness ,出于向后兼容的原因,默认情况下处于关闭状态。

public class XIncludeDemo {

private static final String XML = "<?xml version=\"1.0\"?>\n"
+ "<data xmlns=\"foo\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">\n"
+ "<xi:include href=\"include.txt\" parse=\"text\"/>\n"
+ "</data>\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);
}

}

关于java - Java 6 中对 xinclude 的默认支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/581939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com