I'm looking for a way to merge 2 XML documents - if they have the same attributes - take the elements from the 1st document, but if the elements match, take the value from the 2nd document.
I can get all the elements from the 1st or from the 2nd, but I am unable to merge.
Here are the input.xml the merge.xml and the output.xml, to make myself more clear.
input.xml
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://www.test.com/xmlns/application" name="test"> <services> <service name="service1"> <enabled>true</enabled> <bindings> <binding name="binding1"> <machine>machine1</machine> <product> <type>type1</type> <version>2.0</version> <location>c:/somefolder/1</location> </product> <contact>me</contact> <setting> <startOnBoot>false</startOnBoot> <enableVerbose>false</enableVerbose> <maxLogFileSize>50</maxLogFileSize> <maxLogFileCount>8</maxLogFileCount> <threadCount>1</threadCount> <NTService> <runAsNT>false</runAsNT> <startupType>automatic</startupType> </NTService> <java> <initHeapSize>32</initHeapSize> <maxHeapSize>256</maxHeapSize> <threadStackSize>256</threadStackSize> </java> </setting> <shutdown> <checkpoint>true</checkpoint> <timeout>0</timeout> </shutdown> </binding> </bindings> </service> <service name="service2"> <enabled>false</enabled> <bindings> <binding name="binding1"> <machine>machine2</machine> <product> <type>type2</type> <version>2.1.1.7</version> <location>d:/otherfolder</location> </product> <contact>you</contact> <setting> <startOnBoot>false</startOnBoot> <enableVerbose>false</enableVerbose> <maxLogFileSize>20000</maxLogFileSize> <maxLogFileCount>5</maxLogFileCount> <threadCount>8</threadCount> <NTService> <runAsNT>false</runAsNT> <startupType>automatic</startupType> </NTService> <java> <initHeapSize>32</initHeapSize> <maxHeapSize>256</maxHeapSize> <threadStackSize>256</threadStackSize> </java> </setting> <shutdown> <checkpoint>true</checkpoint> <timeout>5</timeout> </shutdown> </binding> </bindings> </service> </services> </application>
merge.xml
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://www.test.com/xmlns/application" name="test"> <services> <service name="service2"> <enabled>false</enabled> <bindings> <binding name="binding1"> <setting> <startOnBoot>true</startOnBoot> </setting> <shutdown> <timeout>7</timeout> </shutdown> </binding> </bindings> </service> <service name="service1"> <enabled>true</enabled> <bindings> <binding name="binding1"> <product> <type>custom</type> </product> <contact>someoneelse</contact> <setting> <threadCount>10</threadCount> <NTService> <runAsNT>true</runAsNT> </NTService> <java> <initHeapSize>64</initHeapSize> <maxHeapSize>1024</maxHeapSize> </java> </setting> </binding> </bindings> </service> </services> </application>
output.xml
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://www.test.com/xmlns/application" name="test"> <services> <service name="service1"> <enabled>true</enabled> <bindings> <binding name="binding1"> <machine>machine1</machine> <product> <type>custom</type> <version>2.0</version> <location>c:/somefolder/1</location> </product> <contact>someoneelse</contact> <setting> <startOnBoot>false</startOnBoot> <enableVerbose>false</enableVerbose> <maxLogFileSize>50</maxLogFileSize> <maxLogFileCount>8</maxLogFileCount> <threadCount>10</threadCount> <NTService> <runAsNT>true</runAsNT> <startupType>automatic</startupType> </NTService> <java> <initHeapSize>64</initHeapSize> <maxHeapSize>1024</maxHeapSize> <threadStackSize>256</threadStackSize> </java> </setting> <shutdown> <checkpoint>true</checkpoint> <timeout>0</timeout> </shutdown> </binding> </bindings> </service> <service name="service2"> <enabled>false</enabled> <bindings> <binding name="binding1"> <machine>machine2</machine> <product> <type>type2</type> <version>2.1.1.7</version> <location>d:/otherfolder</location> </product> <contact>you</contact> <setting> <startOnBoot>true</startOnBoot> <enableVerbose>false</enableVerbose> <maxLogFileSize>20000</maxLogFileSize> <maxLogFileCount>5</maxLogFileCount> <threadCount>8</threadCount> <NTService> <runAsNT>false</runAsNT> <startupType>automatic</startupType> </NTService> <java> <initHeapSize>32</initHeapSize> <maxHeapSize>256</maxHeapSize> <threadStackSize>256</threadStackSize> </java> </setting> <shutdown> <checkpoint>true</checkpoint> <timeout>7</timeout> </shutdown> </binding> </bindings> </service> </services> </application>
No comments:
Post a Comment