简体   繁体   English

将带有xinclude标记的多个XML文件连接到单个文件中

[英]Join multiple XML files with xinclude tags into single file

I am creating an installer in IzPack. 我正在IzPack中创建安装程序。 It is quite large, and I have broken up my XML files appropriately using <xinclude> and <xfragment> tags. 它很大,我已经使用<xinclude>和<xfragment>标记适当地分解了XML文件。 Unfortunately, IzPack does not combine them together when you build your installer. 不幸的是,在构建安装程序时,IzPack不会将它们结合在一起。 This requires you to package the files with the installer, which just won't work. 这要求您使用安装程序打包文件,而这将无法正常工作。

I was about to start writing a tool in Java to load the XML files and combine them, but I don't want to go reinventing the wheel. 我本来打算用Java编写工具来加载XML文件并组合它们,但是我不想重新发明轮子。

Do the Java XML libraries provide native handling of xinclude? Java XML库是否提供对xinclude的本地处理? A google didn't seem to turn up much. 谷歌似乎没有出现太多。

Not a big deal if I have to write this myself, just wanted to check with you guys. 如果我必须自己写这个,只是想和你们一起检查,没什么大不了的。 Thanks. 谢谢。

Format of XML for example purposes: File1.xml 用于示例目的的XML格式:File1.xml

<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>
<installation version="1.0">
<packs>      
    <pack name="Transaction Service" id="Transaction Service" required="no" >
        <xinclude href="example/File2.xml" />
    </pack>
</packs>

File2.xml File2.xml

<xfragment>
    <file src="..." />
</xfragment>

File2 does not need the standard XML header. File2不需要标准的XML标头。 The xml file is parsed at build time, because the resources it specifies are included in the installer. xml文件是在构建时解析的,因为它指定的资源包含在安装程序中。 What isn't included is the actual XML information (order to write the files, where to put them etc.) 不包括实际的XML信息(写入文件的顺序,放置它们的位置等)。

What I am looking to have produced: 我希望产生的是:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>
<installation version="1.0">
<packs>      
    <pack name="Transaction Service" id="Transaction Service" required="no" >
        <file src="..." />
    </pack>
</packs>

Thanks, I am going to start whipping it together in Java now, but hopefully someone has a simple answer. 谢谢,我现在将开始在Java中一起鞭打它,但希望有人能给出一个简单的答案。

Tim Reynolds 蒂姆·雷诺兹(Tim Reynolds)

If you can't get xinclude to work and you're using Ant, I'd recommend XMLTask , which is a plugin task for Ant. 如果您无法使用xinclude且正在使用Ant,那么我建议您使用XMLTask ,这是Ant的插件任务。 It'll do lots of clever stuff, including the one thing you're interested in - constructing a XML file out of fragments. 它会做很多聪明的事情,包括您感兴趣的一件事-从片段构造XML文件。

eg 例如

<xmltask source="templatefile.xml" dest="finalfile.xml">
  <insert path="/packs/pack[1]" position="under" file="pack1.xml"/>
</xmltask>

(warning- the above is done from memory so please consult the documentation!). (警告-以上内容是通过内存完成的,因此请查阅文档!)。

Note that in the above, the file pack1.xm l doesn't have to have a root node. 请注意,在上面,文件pack1.xm l不必具有根节点。

This works now: 现在可以使用:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>
<installation version="1.0">
<packs>      
    <pack name="Transaction Service" id="Transaction Service" required="no" >
        <xi:include href="example/File2.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
    </pack>
</packs>

example/File2.xml 例如/ File2.xml

<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>
<xfragment>
    <file src="..." />
</xfragment>

Just for anyone who wants to know. 只适合想要知道的人。 IzPack used nanoXML to parse all config files. IzPack使用nanoXML解析所有配置文件。 It does not have namespaces. 它没有名称空间。 And does not handle xml includes. 并且不处理xml包含。

To resolve an issue I had I added the "xinclude" etc (fragment/fallback) element to the parser stuff so that it that mostly followed the standards for x:include (notice the name difference?) One is correct and has a namespace. 为了解决一个问题,我在解析器内容中添加了“ xinclude”等(片段/后备)元素,以便该元素大部分遵循x:include的标准(注意名称差异?)。这是正确的,并且具有名称空间。 The other is a nasty hack that pretends to follow the standard without using namespaces. 另一个是讨厌的黑客,它假装遵循标准而不使用名称空间。

Anyway this is long ago and now IzPack uses a sane XML parser and understands if you do it correctly xi:include or whatever prefix you wish to use there are no problems. 无论如何,这是很久以前的事了,现在IzPack使用了健全的XML解析器,并且了解xi:include或您希望使用的任何前缀是否正确实现了,所以没有问题。 It is standard in decent xml parsers. 它在体面的xml解析器中是标准的。

Apache Xerces, for example, should support Xinclude, but you will need to enable it. 例如,Apache Xerces应该支持Xinclude,但是您需要启用它。

http://xerces.apache.org/xerces2-j/faq-xinclude.html http://xerces.apache.org/xerces2-j/faq-xinclude.html

import javax.xml.parsers.SAXParserFactory;

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setXIncludeAware(true);

Their documentation also says you can enable it as a feature 他们的文档还说您可以将其启用为功能

I'm not sure if java supports automatic xinclude. 我不确定java是否支持自动xinclude。 But you will have to use namespaces to make it work. 但是您将必须使用名称空间才能使其正常工作。 So don't use <xinclude ....> , but use: 因此,请勿使用<xinclude ....> ,而应使用:

<xi:xinclude xmlns:xi="http://www.w3.org/2001/XInclude" href="example/File2.xml" />

Normally the included file should still contain the xml header as well. 通常,所包含的文件也应仍包含xml标头。 There is no requirement for it to eg have the same encoding. 不需要例如具有相同的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM