Hi all, I'm looking for a way to merge two XML files (appending/replacing existing values). I have checked out XML::Merge, but unfortunately it doesn't seem to support merging files with conflicts. I'm thinking I may need to write some code to do this as it is a bit of an odd thing to do, and was hoping someone could advise on which libraries I should use? As an example, this is what I want to do. I have an original file, and a "patch" file, and I want to merge these two files so that any additions in the patch file are put in the new file, and any existing nodes in the patch file are updated. Here is a (crude) example of what I mean:

Original File:

<NODEA> <NODEB UID="111" > <NODEC name="1" > <NODED> text1 </NODED> <NODED> text2 </NODED> </NODEC> </NODEB> <NODEB UID="222" > <NODEC name="2" > <NODED> text1 </NODED> <NODED> text2 </NODED> </NODEC> </NODEB> </NODEA>
Patch File:
<NODEA> <NODEB UID="111" > <NODEC name="1" > <NODED> patched text 1 </NODED> <NODED> patched text 2 </NODED> </NODEC> </NODEB> <NODEB UID="333" > <NODEC name="3" > <NODED> text1 </NODED> <NODED> text2 </NODED> </NODEC> </NODEB> </NODEA>
Merged File:
<NODEA> <NODEB UID="111" > <NODEC name="1" > <NODED> patched text 1 </NODED> <NODED> patched text 2 </NODED> </NODEC> </NODEB> <NODEB UID="222" > <NODEC name="2" > <NODED> text1 </NODED> <NODED> text2 </NODED> </NODEC> </NODEB> <NODEB UID="333" > <NODEC name="3" > <NODED> text1 </NODED> <NODED> text2 </NODED> </NODEC> </NODEB> </NODEA>
I would really appreciate any pointers on what libraries I could use for this, or if there is anything else like XML::Merge that I may have missed. I am thinking of using XML::Twig and to manually build up an array of nodes and compare them (although that sound really messy), but wanted to hear any ideas before I got started.

Thanks!


In reply to Merging two XML files (with conflicts) by pwnguin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.