-1

i am performing xml merge of two xml files. xmlSrc will be merged into xmlDest

Code looks like

def xmlSrc = new XmlSlurper(false,false).parse(srcFile)
def xmlDest = new XmlSlurper(false,false).parse(destFile)
xmlSrc.children().each{
            if(xmlDest.children().contains(it) == false){
                log "Merging entry ${groovy.xml.XmlUtil.serialize(it)}"
                xmlDest << it
            }else{
                log "not merging: ${groovy.xml.XmlUtil.serialize(it)}"
            }
}

Src XML looks like:

<?xml version = '1.0' encoding = 'UTF-8'?>
<MetadataDirectory xmlns="http://xmlns.oracle.com/adfm/metainf" version="11.1.1.0.0">
   <BusinessComponentProjectRegistry path="val1"/>
</MetadataDirectory>

Dest looks like

<?xml version = '1.0' encoding = 'UTF-8'?>
    <MetadataDirectory xmlns="http://xmlns.oracle.com/adfm/metainf" version="11.1.1.0.0">
       <BusinessComponentProjectRegistry path="val2"/>
    </MetadataDirectory>

I am expecting my code to not find a match while merge and insert the node. However, it's always returning true.

Vik
  • 8,721
  • 27
  • 83
  • 168
  • Are you trying to merge both files data in another file? And it is not merging with the code that was shown in the above? – Rao Mar 18 '16 at 06:33
  • Please see if [this](http://stackoverflow.com/questions/13715779/groovy-copy-xml-elements-from-one-doc-to-another) or [other](http://philip.yurchuk.com/software/merging-xml-files-with-groovy-and-talend/) are helpful. – Rao Mar 18 '16 at 06:40
  • no i am merging src file xml into dest file xml – Vik Mar 18 '16 at 06:45

1 Answers1

1

So you need to compare the name and attributes of the nodes, you can't just compare nodes.

xmlSrc.children().each { srcChild ->
    if(xmlDest.children().find { it.name() == srcChild.name() && it.attributes() == srcChild.attributes() }){
        println "not merging: ${XmlUtil.serialize(srcChild)}"
    }
    else {
        println "Merging entry ${XmlUtil.serialize(srcChild)}"
        xmlDest << srcChild
    }
}

And remember, this is a very simplistic solution for this exact use case. In reality, your BusinessComponentProjectRegistry nodes could contain other nodes... How far do you want to walk the trees to find out if they are the same? There might be a difference 5 branches down in the xml for example

tim_yates
  • 167,322
  • 27
  • 342
  • 338