如何使用JAX-B将两个XML合并为一个?

huangapple go评论64阅读模式
英文:

How to merge two XML into one using JAX-B?

问题

我有两个 XML 文件(A.xml 和 B.xml):

<Root>
    <A>
        <name>number</name>
        <value>8</value>
    </A>
</Root>

<Root>
    <A>
        <name>number</name>
        <value>15</value>
    </A>
</Root>

我应该得到结果 XML:

<Root>
    <A>
        <name>number</name>
        <value>8</value>
    </A>
    <A>
        <name>number</name>
        <value>15</value>
    </A>
</Root>

我有自己的解决方案,即从 XML 中获取对象,将其放入列表中,然后从该列表中获取 XML。也许有更好的解决方案(使用 JAX-B)?

英文:

I have two xml files (A.xml and B.xml):

&lt;Root&gt;
    &lt;A&gt;
        &lt;name&gt;number&lt;/name&gt;
        &lt;value&gt;8&lt;/value&gt;
    &lt;/A&gt;
&lt;/Root&gt;

and

&lt;Root&gt;
    &lt;A&gt;
        &lt;name&gt;number&lt;/name&gt;
        &lt;value&gt;15&lt;/value&gt;
    &lt;/A&gt;
&lt;/Root&gt;

And I should to get the result XML:

&lt;Root&gt;
    &lt;A&gt;
        &lt;name&gt;number&lt;/name&gt;
        &lt;value&gt;8&lt;/value&gt;
    &lt;/A&gt;
    &lt;A&gt;
        &lt;name&gt;number&lt;/name&gt;
        &lt;value&gt;15&lt;/value&gt;
    &lt;/A&gt;
&lt;/Root&gt;

I have my own solution is to get the object from XML, put it into a list and then get XML from this list. Maybe there is a better solution (using JAX-B)?

答案1

得分: 3

以下是翻译好的内容:

我不确定为什么您想要在这种情况下使用JAXB,这似乎过于复杂。

您可以使用一行XQuery来完成这个操作:

<Root>{doc('A.xml')/*/*, doc('B.xml')/*/*}</Root>

或者稍微长一点的XSLT:

<Root xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:copy-of select="document('A.xml')/*/* | document('B.xml')/*/*"/>
</Root>
英文:

I'm not sure why you would want to use JAXB for this, it seems over-complex.

You can do this with a one-line XQuery

&lt;Root&gt;{doc(&#39;A.xml&#39;)/*/*, doc(&#39;B.xml&#39;)/*/*}&lt;/Root&gt;

Or with an only slightly longer XSLT:

&lt;Root xsl:version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:copy-of select=&quot;document(&#39;A.xml&#39;)/*/* | document(&#39;B.xml&#39;)/*/*&quot;/&gt;
&lt;/Root&gt;

huangapple
  • 本文由 发表于 2020年10月7日 23:06:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64246933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定