递归合并两个XML文件 [英] Merge two XML files recursively

查看:125
本文介绍了递归合并两个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to merge 2 XML files into one recursively. For example :

第一个文件:

<root>
    <branch1>
        <node1>Test</node1>
    </branch1>
    <branch2>
        <node>Node from 1st file</node>
    </branch2>
</root>

第二个文件:

<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>

合并文件:

<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>

我希望将第二个文件添加到第一个文件.当然,可以在XML的任何深度进行合并.

I want the second file to be added to the first file. Of course the merging can be done with any depth of the XML.

我在Google上进行了搜索,但找不到能正常工作的脚本.

I have searched on Google and didn't found a script that worked properly.

你能帮我吗?

推荐答案

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

xml2array is a function that converts an xml document to an array. Once the two arrays are created, you can use array_merge_recursive to merge them. Then you can convert the array back to xml with XmlWriter (should already be installed).

这篇关于递归合并两个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆