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

Viewed 90

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

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

and

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

And I should to get the result XML:

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

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 Answers

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

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

Or with an only slightly longer 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>
Related