Java XML 解析 - 合并 xi:include 的输出

2023-12-24

我有一个 XML 文件,其中包含以下内容:

<contexts>
    <context name="a">
        <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" href="reuse.xml"/>
        <other_stuff/>
    </context>
    <context name="b">
        <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" href="reuse.xml"/>
        <other_stuff/>
    </context>
</contexts>

...以及上面的 XInclude 引用的reuse.xml,其中包含如下内容:

<good_stuff/><!-- and a bunch of complex stuff ->

我正在尝试编写一些Java代码,它可以为每个上下文元素生成一些字符串输出(简单),但不是使用 xi:include 行,而是将其替换为包含文件的内容,即为每个元素提供一个“分解”字符串上下文是这样的:

<context name="a">
    <good_stuff/><!-- and a bunch of complex stuff ->
    <other_stuff/>
</context>

而不是我现在得到的:

<context name="a">
    <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" href="reuse.xml"/> 
    <other_stuff/>
</context>

希望我已经清楚地解释了自己,我不希望输出中包含 xi:include 文本,而是应该替换它的其他文件的内容。

我将解析器设置为 XInclude 和命名空间感知:

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

...

然后获取我想要的类型的子节点(在本例中类型为我提供了上下文)。

NodeList result = doc.getElementsByTagName(type).item(0).getChildNodes();

但是当我迭代这个,并使用以下内容将每个元素传递给 StringWriter 时,它并没有替换 xi:include;

StringWriter sw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "no");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty(OutputKeys.STANDALONE, "yes");
serializer.transform(new DOMSource(element), new StreamResult(sw));
String result = sw.toString();

除了使用字符串操作来替换 xi:include 之外,还有其他方法可以使用 JDK 7/8 内置 XML 解析器来执行此操作吗?

感谢您的帮助!


我认为您使用了错误的命名空间XInclude参考,当我更改要使用的 XML 时

<?xml version="1.0" encoding="UTF-8"?>
<contexts>
    <context name="a">
        <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="reuse.xml"/>
        <other_stuff/>
    </context>
    <context name="b">
        <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="reuse.xml"/>
        <other_stuff/>
    </context>
</contexts>

它使用命名空间http://www.w3.org/2001/XIncludeW3C XInclude 规范中定义https://www.w3.org/TR/xinclude/#syntax https://www.w3.org/TR/xinclude/#syntax,并使用示例 XML 文档

<?xml version="1.0" encoding="UTF-8"?>
<good_stuff/>

然后Java代码输出例如

<context name="a">
        <good_stuff xml:base="reuse.xml"/>
        <other_stuff/>
    </context>
<context name="b">
        <good_stuff xml:base="reuse.xml"/>
        <other_stuff/>
    </context>

当使用默认变压器输出时context要素:

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

    DocumentBuilder docBuilder = factory.newDocumentBuilder();

    Document doc = docBuilder.parse("file1.xml");

    NodeList contextElements = doc.getElementsByTagName("context");

    for (int i = 0, l = contextElements.getLength(); i < l; i++) {
        Node element = contextElements.item(i);
        StringWriter sw = new StringWriter();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "no");
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty(OutputKeys.STANDALONE, "yes");

        serializer.transform(new DOMSource(element), new StreamResult(sw));
        String result = sw.toString();
        System.out.println(result);
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java XML 解析 - 合并 xi:include 的输出 的相关文章

随机推荐