合并2个XML文件

时间:2015-11-18 21:08:53

标签: xml merge

我已经尝试了很多不同的东西,合并2个XML文件,例如XSTL 1.0。但我仍然没有100%的能力。它需要在Linux和Windows上运行,到现在我并不关心它是如何工作的,它只需要工作。所以,如果它使用工具,jar,xstl 1.0,2.0,3.0或其他什么。只要它工作简单且易于更改,我就可以了。它应该尽可能通用。

基本上,输入XML包含完整的XML结构。 如果名称匹配(并且属性具有相同的值),则应从第二个文件中获取该值。如果第二个XML包含一个新元素,则应该添加它。

此外,XML文件中的不同位置还有NVPairs(名称值对)。如果名称匹配,则应从第二个文件中获取值字段的值。如果第二个文件包含新的名称值对,则应将其添加到结果中。

我已经找到了一个简化的例子。

输入:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://www.test.com/xmlns/Test" name="TEST">
    <repoInstanceName>testRepo</repoInstanceName>
    <NVPairs name="Global Variables">
        <NameValuePair>
            <name>Host</name>
            <value>localhost</value>
        </NameValuePair>
        <NameValuePairInteger>
            <name>Timeout</name>
            <value>60</value>
        </NameValuePairInteger>
    </NVPairs>
    <repoInstances selected="local">
        <localRepoInstance>
            <encoding>UTF-8</encoding>
        </localRepoInstance>
    </repoInstances>
    <services>
        <bw name="1.par">
            <enabled>true</enabled>
            <bindings>
                <binding name="1">
                    <machine>localhost</machine>
                    <product>
                        <type>testtype</type>
                        <version>12.9</version>
                    </product>
                    <contact>mailme</contact>
                    <setting>
                        <enableVerbose>false</enableVerbose>
                        <threadCount>12</threadCount>
                        <java>
                            <initHeapSize>128</initHeapSize>
                            <maxHeapSize>256</maxHeapSize>
                            <threadStackSize>512</threadStackSize>
                        </java>
                    </setting>
                    <shutdown>
                        <checkpoint>true</checkpoint>
                        <timeout>0</timeout>
                    </shutdown>
                </binding>
            </bindings>
            <NVPairs name="Adapter SDK Properties">
                <NameValuePair>
                    <name>Trace.Task.*</name>
                    <value>false</value>
                </NameValuePair>
                <NameValuePair>
                    <name>EnableMemorySavingMode</name>
                    <value>false</value>
                </NameValuePair>
            </NVPairs>
            <failureCount>0</failureCount>
            <failureInterval>0</failureInterval>
            <bwprocesses>
                <bwprocess name="process1">
                    <starter>name</starter>
                    <enabled>true</enabled>
                    <maxJob>8</maxJob>
                    <activation>true</activation>
                    <flowLimit>8</flowLimit>
                </bwprocess>
            </bwprocesses>
            <checkpoints selected="Local File">
                <checkpoint>Local File</checkpoint>
                <checkpoint>/lalaa/</checkpoint>
                <tablePrefix>ffffl_7787</tablePrefix>
            </checkpoints>
            <isFt>false</isFt>
        </bw>
        <bw name="2.par">
            <enabled>true</enabled>
            <bindings>
                <binding name="2">
                    <machine>other</machine>
                    <product>
                        <type>testtype2</type>
                        <version>12.9</version>
                    </product>
                    <contact>mailme</contact>
                    <setting>
                        <enableVerbose>false</enableVerbose>
                        <threadCount>12</threadCount>
                        <java>
                            <initHeapSize>128</initHeapSize>
                            <maxHeapSize>256</maxHeapSize>
                            <threadStackSize>512</threadStackSize>
                        </java>
                    </setting>
                    <shutdown>
                        <checkpoint>true</checkpoint>
                        <timeout>0</timeout>
                    </shutdown>
                </binding>
            </bindings>
            <NVPairs name="Adapter SDK Properties">
                <NameValuePair>
                    <name>Trace.Task.*</name>
                    <value>false</value>
                </NameValuePair>
                <NameValuePair>
                    <name>EnableMemorySavingMode</name>
                    <value>false</value>
                </NameValuePair>
            </NVPairs>
            <failureCount>0</failureCount>
            <failureInterval>0</failureInterval>
            <bwprocesses>
                <bwprocess name="process1">
                    <starter>name</starter>
                    <enabled>true</enabled>
                    <maxJob>8</maxJob>
                    <activation>true</activation>
                    <flowLimit>8</flowLimit>
                </bwprocess>
                <bwprocess name="process2">
                    <starter>name</starter>
                    <enabled>true</enabled>
                    <maxJob>8</maxJob>
                    <activation>true</activation>
                    <flowLimit>8</flowLimit>
                </bwprocess>                
            </bwprocesses>
            <checkpoints selected="Local File">
                <checkpoint>Local File</checkpoint>
                <checkpoint>/lalaa/</checkpoint>
                <tablePrefix>ffffl_7787</tablePrefix>
            </checkpoints>
            <isFt>false</isFt>
        </bw>       
    </services>
</application>

replaceOrAdd.xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://www.test.com/xmlns/Test" name="TEST">
    <NVPairs name="Global Variables">
        <NameValuePair>
            <name>Host</name>
            <value>newhost</value>
        </NameValuePair>
        <NameValuePairPassword>
            <name>PasswordDB</name>
            <value>secret</value>
        </NameValuePairPassword>        
    </NVPairs>
    <repoInstances selected="local">
        <localRepoInstance>
            <encoding>UTF-16</encoding>
            <newParameter>ADD_ME</newParameter>
        </localRepoInstance>
    </repoInstances>
    <services>
        <bw name="1.par">
            <enabled>false</enabled>
            <bindings>
                <binding name="1">
                    <machine>testRepo</machine>
                    <product>
                        <type>testtype122</type>
                    </product>
                    <setting>
                        <threadCount>7</threadCount>
                        <java>
                            <initHeapSize>32</initHeapSize>
                            <maxHeapSize>1024</maxHeapSize>
                        </java>
                    </setting>
                    <shutdown>
                        <timeout>7</timeout>
                    </shutdown>
                </binding>
            </bindings>
            <NVPairs name="Adapter SDK Properties">
                <NameValuePair>
                    <name>EnableMemorySavingMode</name>
                    <value>true</value>
                </NameValuePair>
            </NVPairs>
            <checkpoints selected="Local File">
                <checkpoint>Other File</checkpoint>
            </checkpoints>
        </bw>
        <bw name="2.par">
            <enabled>false</enabled>
            <bwprocesses>
                <bwprocess name="process1">
                    <activation>false</activation>
                    <flowLimit>16</flowLimit>
                </bwprocess>
                <bwprocess name="process2">
                    <enabled>false</enabled>
                </bwprocess>                
            </bwprocesses>
            <isFt>true</isFt>
        </bw>       
    </services>
</application>

的Output.xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://www.test.com/xmlns/Test" name="TEST">
    <repoInstanceName>testRepo</repoInstanceName>
    <NVPairs name="Global Variables">
        <NameValuePair>
            <name>Host</name>
            <value>newhost</value>
        </NameValuePair>
        <NameValuePairInteger>
            <name>Timeout</name>
            <value>60</value>
        </NameValuePairInteger>
        <NameValuePairPassword>
            <name>PasswordDB</name>
            <value>secret</value>
        </NameValuePairPassword>        
    </NVPairs>
    <repoInstances selected="local">
        <localRepoInstance>
            <encoding>UTF-16</encoding>
            <newParameter>ADD_ME</newParameter>
        </localRepoInstance>
    </repoInstances>
    <services>
        <bw name="1.par">
            <enabled>false</enabled>
            <bindings>
                <binding name="1">
                    <machine>testRepo</machine>
                    <product>
                        <type>testtype122</type>
                        <version>12.9</version>
                    </product>
                    <contact>mailme</contact>
                    <setting>
                        <enableVerbose>false</enableVerbose>
                        <threadCount>7</threadCount>
                        <java>
                            <initHeapSize>32</initHeapSize>
                            <maxHeapSize>1024</maxHeapSize>
                            <threadStackSize>512</threadStackSize>
                        </java>
                    </setting>
                    <shutdown>
                        <checkpoint>true</checkpoint>
                        <timeout>7</timeout>
                    </shutdown>
                </binding>
            </bindings>
            <NVPairs name="Adapter SDK Properties">
                <NameValuePair>
                    <name>Trace.Task.*</name>
                    <value>false</value>
                </NameValuePair>
                <NameValuePair>
                    <name>EnableMemorySavingMode</name>
                    <value>true</value>
                </NameValuePair>
            </NVPairs>
            <failureCount>0</failureCount>
            <failureInterval>0</failureInterval>
            <bwprocesses>
                <bwprocess name="process1">
                    <starter>name</starter>
                    <enabled>true</enabled>
                    <maxJob>8</maxJob>
                    <activation>true</activation>
                    <flowLimit>8</flowLimit>
                </bwprocess>
            </bwprocesses>
            <checkpoints selected="Local File">
                <checkpoint>Other File</checkpoint>
                <checkpoint>/lalaa/</checkpoint>
                <tablePrefix>ffffl_7787</tablePrefix>
            </checkpoints>
            <isFt>false</isFt>
        </bw>
        <bw name="2.par">
            <enabled>false</enabled>
            <bindings>
                <binding name="2">
                    <machine>other</machine>
                    <product>
                        <type>testtype2</type>
                        <version>12.9</version>
                    </product>
                    <contact>mailme</contact>
                    <setting>
                        <enableVerbose>false</enableVerbose>
                        <threadCount>12</threadCount>
                        <java>
                            <initHeapSize>128</initHeapSize>
                            <maxHeapSize>256</maxHeapSize>
                            <threadStackSize>512</threadStackSize>
                        </java>
                    </setting>
                    <shutdown>
                        <checkpoint>true</checkpoint>
                        <timeout>0</timeout>
                    </shutdown>
                </binding>
            </bindings>
            <NVPairs name="Adapter SDK Properties">
                <NameValuePair>
                    <name>Trace.Task.*</name>
                    <value>false</value>
                </NameValuePair>
                <NameValuePair>
                    <name>EnableMemorySavingMode</name>
                    <value>false</value>
                </NameValuePair>
            </NVPairs>
            <failureCount>0</failureCount>
            <failureInterval>0</failureInterval>
            <bwprocesses>
                <bwprocess name="process1">
                    <starter>name</starter>
                    <enabled>true</enabled>
                    <maxJob>8</maxJob>
                    <activation>false</activation>
                    <flowLimit>16</flowLimit>
                </bwprocess>
                <bwprocess name="process2">
                    <starter>name</starter>
                    <enabled>false</enabled>
                    <maxJob>8</maxJob>
                    <activation>true</activation>
                    <flowLimit>8</flowLimit>
                </bwprocess>                
            </bwprocesses>
            <checkpoints selected="Local File">
                <checkpoint>Local File</checkpoint>
                <checkpoint>/lalaa/</checkpoint>
                <tablePrefix>ffffl_7787</tablePrefix>
            </checkpoints>
            <isFt>true</isFt>
        </bw>       
    </services>
</application>

0 个答案:

没有答案