1

For example, say I have two XML Strings that I want to merge:

<Test>
    <A>false</A>
    <B>true</B>
    <C>Transfers!</C>
</Test>

<Test2>
    <A>false</A>
    <B>false</B>
</Test2>

Where the final result looks like (notice, B is now false, because I would like a place to do some sort of logic to determine the final "merged values", in this example, a method would take in two strings, false, and true, and do some AND logic and return "false")

<Test>
    <A>false</A>
    <B>false</B>
    <C>Transfers!</C>
</Test>

I am thinking of either doing it all by hand, using JAX-B & XPath, or XStream Marshal/Unmarshallers. Does anyone have an example of the best & most dynamic way to do this?

The element names will change, they will not always be "A" and "B", but the two XML strings I pass in will always have some elements in common, but sometimes one XML String/Document may have one or more elements that don't exist in the other document, and in this case those need to be in the final XML result as is.

10
  • You would create the final result the same way you created the two original XML strings (albeit with some processing in-between). Aug 10, 2016 at 20:11
  • Assume I am not creating the two original XML Strings, but want to merge them myself with some logic in between :) Aug 10, 2016 at 20:17
  • Then you'll first need to re-create the process by which you can read and write those XML strings. Generally, you'll have an object that contains the boolean values corresponding to the A and B elements, and a serialization and deserialization process that converts the object to and from the XML string. Once you have that, the final solution should be trivial. Unless, of course, you want to go the whole "parse it by hand, write it back out by hand" route. But you still need to do some math, so having object representations makes that much easier. Aug 10, 2016 at 20:19
  • I know what I need to do "in theory", my question was what's the most dynamic way to do this with possible samplecode :) Aug 10, 2016 at 20:21
  • What do you mean by "dynamic?" Aug 10, 2016 at 20:21

2 Answers 2

1

XML and JaxB will allow you to unmarshall these xml strings/documents into objects. After that you would feed these two objects as input to a Merge(thing1,thing2) method that then returns the merged object. You then marshall into an xml string/document and you are complete.

As far as making it "dynamic", that wouldn't be prudent. You would want to define logic on how each of the potential fields is populated (assuming there will be non-boolean fields).

Note: the marshal/unmarshal gives an opportunity to enforce the schema on the given data. If you parse the document by hand then you will be forced to validate the xml formatting and validate against a schema, which doesn't sound like a great idea considering that there are libraries out there that every soap web service is already using.

0

If you have variety of needs of copying/pasting parts of XML messages, in flexible way (specified in configuration), you can use XSL Transformations (Admitted that it may be overkill to introduce XSLT into your project for tiny bit of requirement).

Input XML document:

<?xml version="1.0" encoding="UTF-8"?>
<Wrapper>
    <Test>
            <title>Empire Burlesque</title>
            <country>USA</country>
    </Test>
    <Test1>
            <title>Hide your heart</title>
            <country>UK</country>
    </Test1>
    <Test2>
            <title>Go to Heaven</title>
            <country>Australia</country>
    </Test2>
    <DefaultTest>
        <title>Default Title</title>
        <country>Default Country</country>
    </DefaultTest>
</Wrapper>

XSLT document:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/*">
    <xsl:for-each select="*[starts-with(name(),'Test')]">
        <xsl:copy-of select="*"/>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

The resultant Output:

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

<title>Empire Burlesque</title>
<country>USA</country>

<title>Hide your heart</title>
<country>UK</country>

<title>Go to Heaven</title>
<country>Australia</country>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.