Java XML解析 - 合并xi:include的输出 [英] Java XML parsing - merging the output of xi:include

查看:363
本文介绍了Java XML解析 - 合并xi:include的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,具有以下内容:

I have an XML file that has the following:

<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包含以下内容: p>

... and the reuse.xml referenced by the XInclude above which contains something like this:

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

我正在尝试编写一些可以为每个上下文元素生成一些字符串输出的Java代码),而不是xi:include行,将其替换为所包含的文件的内容,即为每个上下文提供一个分解字符串,如下所示:

I'm trying to write some Java code which can generate some string output for each context element (easy) but instead of the xi:include line, replace that with the contents of the included file i.e. give me an 'exploded' String for each Context like this:

<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:包含文本在我的输出,而是其他文件的内容应该被替换。

Hopefully I've explained myself clearly that I don't want the xi:include text in my output, but rather the contents of the other file it should be replaced by.

我将解析器设置为XInclude和Namespace:

I'm setting the parser to be XInclude and Namespace aware:

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

...

然后得到我想要的类型的子节点(在这种情况下,类型是给我上下文)。

Then getting the child nodes of the type I want (type is giving me the Context in this case).

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

但是,当我迭代这个,并将每个元素传递给StringWriter与以下,它是'替换xi:include;

But when I iterate over this, and pass each element to the StringWriter with the following, it isn't replacing the 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();

有没有办法使用JDK 7/8内置的XML解析器,除了字符串操作来替换xi:包括我自己?

Is there a way to do this with the JDK 7/8 built-in XML parsers, other than String manipulation to replace the xi:include myself?

感谢任何帮助!

推荐答案

当我将XML更改为使用

I think you have used the wrong namespace for the XInclude reference, when I change the XML to use

<?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 / XInclude 在W3C XInclude规范中定义 https://www.w3 .org / TR / xinclude /#syntax ,并使用示例XML文档

which uses the namespace http://www.w3.org/2001/XInclude defined in the W3C XInclude spec https://www.w3.org/TR/xinclude/#syntax, and use a sample XML document

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

然后Java代码输出例如

then the Java code outputs e.g.

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

当使用默认变压器输出上下文元素:

when using a default transformer to output the context elements:

    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);
    }

这篇关于Java XML解析 - 合并xi:include的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆