以递归方式合并两个XML文件

Kyr*_*yus 8 php xml merge simplexml

我想以递归方式将2个XML文件合并为一个.例如 :

第一档:

<root>
    <branch1>
        <node1>Test</node1>
    </branch1>
    <branch2>
        <node>Node from 1st file</node>
    </branch2>
</root>
Run Code Online (Sandbox Code Playgroud)

第二档:

<root>
    <branch1>
        <node2>Test2</node2>
    </branch1>
    <branch2>
        <node>This node should overwrite the 1st file branch</node>
    </branch2>
    <branch3>
        <node>
            <subnode>Yeah</subnode>
        </node>
    </branch3>
</root>
Run Code Online (Sandbox Code Playgroud)

合并文件:

<root>
    <branch1>
        <node1>Test</node1>
        <node2>Test2</node2>
    </branch1>
    <branch2>
        <node>This node should overwrite the 1st file branch</node>
    </branch2>
    <branch3>
        <node>
            <subnode>Yeah</subnode>
        </node>
    </branch3>
</root>
Run Code Online (Sandbox Code Playgroud)

我想将第二个文件添加到第一个文件中.当然,可以使用任何深度的XML来完成合并.

我在Google上搜索过,但没有找到一个正常运行的脚本.

你能帮我吗 ?

Exp*_*lls 4

xml2array是将 xml 文档转换为数组的函数。创建两个数组后,您可以使用array_merge_recursive它们来合并它们。XmlWriter然后您可以使用(应该已经安装)将数组转换回 xml 。