Merge XML Documents with PHP recursive



i have a question about merging xml documents. I need to merge two xml documents to one. The second document have a higher priority, when in the second document are nodes that in the first document not exists.


So at example when i have two documents like this:




  1. Example



    <root>
    <object3>
    <ObjectX action="show">
    <name>ABC</name>
    <type>XYZ</type>
    </ObjectX>
    </object3>



  2. Example



    <root>
    <object1>
    <attr>OK</attr>
    <attr2>200</attr2>
    </object1>
    <object2>
    <item>
    <x>a</x>
    <y>b</y>
    </item>
    <item>
    <x>c</x>
    <y>d</y>
    </item>
    <item>
    <x>e</x>
    <y>f</y>
    </item>
    </object2>



So when i have this two documents, i need to merge them to one. Some nodes will be handled as complex objects. So the goal that i have is to merge this xml documents to at example (above Examples):



<root>
<object1>
<attr>OK</attr>
<attr2>200</attr2>
</object1>
<object2>
<item>
<x>a</x>
<y>b</y>
</item>
<item>
<x>c</x>
<y>d</y>
</item>
<item>
<x>e</x>
<y>f</y>
</item>
</object2>
<object3>
<ObjectX action="show">
<name>ABC</name>
<type>XYZ</type>
</ObjectX>
</object3>
</root>


It could happen, that a subobject have already other filled noded in first document, and second have also this nodes but not filled with values. When this happens, should a merging script only merge values wich are not filled with values from filled document.


A Conflict by xml nodes can't happen in the case that i want to implement.


Is there a library for doing this? Maybe for ZF2? Or could anyone else help me out?


No comments:

Post a Comment