作者:玩偶0-0 | 来源:互联网 | 2023-05-19 14:20
IhavetwoXMLfilesofsimilarstructurewhichIwishtomergeintoonefile.CurrentlyIamusing
I have two XML files of similar structure which I wish to merge into one file. Currently I am using EL4J XML Merge which I came across in this tutorial. However it does not merge as I expect it to for instances the main problem is its not merging the from both files into one element aka one that contains 1, 2, 3 and 4. Instead it just discards either 1 and 2 or 3 and 4 depending on which file is merged first.
我有两个结构相似的XML文件,我希望将它们合并到一个文件中。目前我使用的是我在本教程中遇到的EL4J XML Merge。然而它并没有像我期望的那样合并实例主要问题是它没有将两个文件合并为一个元素,即包含1,2,3和4的元素。相反,它只丢弃1和2或3和4取决于首先合并的文件。
So I would be grateful to anyone who has experience with XML Merge if they could tell me what I might be doing wrong or alternatively does anyone know of a good XML API for Java that would be capable of merging the files as I require?
所以我会感谢任何有XML Merge经验的人,如果他们可以告诉我我可能做错了什么,或者有没有人知道一个优秀的XML API for Java能够根据我的要求合并文件?
Many Thanks for Your Help in Advance
非常感谢您的帮助
Edit:
编辑:
Could really do with some good suggestions on doing this so added a bounty. I've tried jdigital's suggestion but still having issues with XML merge.
真的可以做一些关于这样做的好建议,所以增加了赏金。我已经尝试过jdigital的建议,但仍然遇到XML合并的问题。
Below is a sample of the type of structure of XML files that I am trying to merge.
下面是我尝试合并的XML文件结构类型的示例。
Expected output
预期产出
11 个解决方案
6
I use XSLT to merge XML files. It allows me to adjust the merge operation to just slam the content together or to merge at an specific level. It is a little more work (and XSLT syntax is kind of special) but super flexible. A few things you need here
我使用XSLT来合并XML文件。它允许我调整合并操作,只是将内容一起猛击或在特定级别合并。这是一个更多的工作(和XSLT语法是一种特殊的),但超级灵活。这里有一些你需要的东西
a) Include an additional file b) Copy the original file 1:1 c) Design your merge point with or without duplication avoidance
a)包含一个附加文件b)复制原始文件1:1 c)设置合并点,有或没有避免重复
a) In the beginning I have
a)一开始我有
yoursecondfile.xml
this allows to point to the second file using $mDoc
这允许使用$ mDoc指向第二个文件
b) The instructions to copy a source tree 1:1 are 2 templates:
b)复制源树1:1的说明是2个模板:
With nothing else you get a 1:1 copy of your first source file. Works with any type of XML. The merging part is file specific. Let's presume you have event elements with an event ID attribute. You do not want duplicate IDs. The template would look like this:
没有其他任何东西你得到你的第一个源文件的1:1副本。适用于任何类型的XML。合并部分是特定于文件的。让我们假设你有一个带有事件ID属性的事件元素。您不需要重复的ID。模板看起来像这样:
Of course you can compare other things like tag names etc. Also it is up to you how deep the merge happens. If you don't have a key to compare, the construct becomes easier e.g. for log:
当然,您可以比较标签名称等其他内容。此外,合并发生的深度取决于您。如果您没有要比较的密钥,则构造变得更容易,例如对于日志:
To run XSLT in Java use this:
要在Java中运行XSLT,请使用以下命令:
Source xmlSource = new StreamSource(xmlFile);
Source xsltSource = new StreamSource(xsltFile);
Result xmlResult = new StreamResult(resultFile);
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
// Load Parameters if we have any
if (ParameterMap != null) {
for (Entry curParam : ParameterMap.entrySet()) {
trans.setParameter(curParam.getKey(), curParam.getValue());
}
}
trans.transform(xmlSource, xmlResult);
or you download the Saxon SAX Parser and do it from the command line (Linux shell example):
或者您下载Saxon SAX Parser并从命令行执行(Linux shell示例):
#!/bin/bash
notify-send -t 500 -u low -i gtk-dialog-info "Transforming $1 with $2 into $3 ..."
# That's actually the only relevant line below
java -cp saxon9he.jar net.sf.saxon.Transform -t -s:$1 -xsl:$2 -o:$3
notify-send -t 1000 -u low -i gtk-dialog-info "Extraction into $3 done!"
YMMV
因人而异