Groovy web console

subscribe to the feed Subscribe
to this
site
merge xml nodes (via #groovywebconsole)
tweet this snippet Tweet
this
script

merge xml nodes

Published 3 months ago by nmrao with tags merge xml nodes
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def xml1 = """<root><Items>
<Item>
<MsgData>
<BahElements>
<Fr>
<Name>
<Address>
<Value>123 Fake St xml1</Value>
</Address>
</Name>
</Fr>
<To>
<Name>
<Address>
<Value>123 Fake St xml1</Value>
</Address>
</Name>
</To>
<MsgDefIdr>MSG_DEF_IDR_VALUE xml1</MsgDefIdr>
</BahElements>
</MsgData>
</Item>
<Item>
<MsgData>
<BahElements>
<Fr>
<Name>
<Address>
<Value>123 Fake St xml1a</Value>
</Address>
</Name>
</Fr>
<To>
<Name>
<Address>
<Value>321 Fake St xml1</Value>
</Address>
</Name>
</To>
<MsgDefIdr>MSG_DEF_IDR_VALUE xml1</MsgDefIdr>
</BahElements>
</MsgData>
</Item>
</Items>
</root>"""
 
def xml2="""<root>
<Items>
<Item>
<MsgData>
<BahElements>
<Fr>
<Name>
<Address>
<Value>123 Fake St xml2a</Value>
</Address>
</Name>
</Fr>
<To>
<Name>
<Address>
<Value>123 Fake St xml2a</Value>
</Address>
</Name>
</To>
<MsgDefIdr>MSG_DEF_IDR_VALUE xml2a</MsgDefIdr>
</BahElements>
</MsgData>
</Item>
<Item>
<MsgData>
<BahElements>
<Fr>
<Name>
<Address>
<Value>123 Fake St xml2b</Value>
</Address>
</Name>
</Fr>
<To>
<Name>
<Address>
<Value>123 Fake St xml2b</Value>
</Address>
</Name>
</To>
<MsgDefIdr>MSG_DEF_IDR_VALUE xml2b</MsgDefIdr>
</BahElements>
</MsgData>
</Item>
</Items>
</root>"""
 
def getItems = { xml -> xml.'**'.findAll{it.name() == 'Item'} }
 
def pXml1 = new XmlSlurper().parseText(xml1)
def pXml2 = new XmlSlurper().parseText(xml2)

getItems(pXml2)?.collect{ pXml1.Items.appendNode(it)}
println groovy.xml.XmlUtil.serialize(pXml1)