首页 > 解决方案 > 使用 SAXParser 合并两个 xml 文件,方法是避免重复节点并在节点重复时添加新属性在 Java 代码中

问题描述

我需要帮助在 java 中合并两个 xml 文件,如果 file2.xml 中有任何新的 mbean 标记,我应该添加,如果应该忽略 100% 重复节点,那么如果现有节点具有 axtra 属性,我需要合并只有属性而不是完整的节点。

file1.xml
<system name="" ostype="DummyOs" version="1">
    <mbean component="CIM" type="" mbclass="" cimclass="DummyClass1">
        <attribute id="Manufacturer" type="text" value="" />
    </mbean>
    <mbean component="CIM" type="" mbclass="" cimclass="DummyClass2>
        <attribute id="Manufacturer" type="text" value="" />
    </mbean>
</system>

file2.xml
<system name="" ostype="dummyOs" version="1">
     <mbean component="CIM" type="" mbclass="" cimclass="DummyClass1">
        <attribute id="Manufacturer" type="text" value="" />
        <attribute id="New_Manufacturer" type="text" value="" /></mbean>
    <mbean component="CIM" type="" mbclass="" cimclass="DummyClass2>
        <attribute id="Manufacturer" type="text" value="" />
    </mbean>
</system>

output.xml
<system name="" ostype="DummyOs" version="1">
    <mbean component="CIM" type="" mbclass="" cimclass="DummyClass1">
        <attribute id="Manufacturer" type="text" value="" />
        <attribute id="New_Manufacturer" type="text" value="" /> 
    </mbean>
    <mbean component="CIM" type="" mbclass="" cimclass="DummyClass2>
        <attribute id="Manufacturer" type="text" value="" />
    </mbean>
</system>

标签: xmlmerge

解决方案


推荐阅读