Could you tell me what is the easiest way to append a XML subtree to a given XML, at a given place?
Example :
def xml1 = ‘’’
<root>
<node1>
<subnode1_1>xyz</subnode1_1>
<subnode1_2></subnode1_2>
</node1>
<node2>
<subnode2_1>xyz</subnode2_1>
</node2>
</root>
‘’’
def xml2 = ‘’’
<node3>
<subnode3_1>sn31</subnode3_1>
<subnode3_2>sn32</subnode3_2>
</node3>
‘’’
Result :
def xml3 = ‘’’
<root>
<node1>
<subnode1_1>xyz</subnode1_1>
<subnode1_2></subnode1_2>
</node1>
<node2>
<subnode2_1>xyz</subnode2_1>
</node2>
<node3>
<subnode3_1>sn31</subnode3_1>
<subnode3_2>sn32</subnode3_2>
</node3>
</root>
‘’’
Should I use XmlParser, XmlSlurper, MarkupBuilder, DOMBuilder…? and how?
Thank you,
Frédéric.
I did a couple of entries earlier this month because I wanted to know a little bit more about processing xml with groovy. Here I leave you the entry. One of them is focused on XmlTemplateEngine.:
http://desmontandojava.blogspot.ie/2013/04/groovy-xml-series-index.html
Anyway I don't think I'm gonna clarify much more than the Groovy documentation. Anyway I hope it helps.
Hi Guillaume,
Yes I looked at the documentation, which gives good examples for parsing or creating an XML from scratch, for modifying, removing or adding a node in a XML tree, but does not explain how to add a whole XML subtree in another XML tree.
Moreover, I have to work with a SOAP XML, so to deal with several namespaces, and in the sopaUI tool, so I need to be able to get/set XML trees from soapUI requests.
I finally found a solution, but it is requires a lot of code and is not really “groovy-like”:
def grUtils = new com.eviware.soapui.support.GroovyUtils(context)
def requestHolder = grUtils.getXmlHolder("myRequest#Request")
def inscriptionNode = requestHolder.getDomNode("//*:Envelope/*:Body/*:InscriptionRequete/*:Inscription")
def namespace = inscriptionNode.getNamespaceURI()
def requestDoc = inscriptionNode.getOwnerDocument()
def verificationNode = requestHolder.getDomNode("//*:Envelope/*:Body/*:InscriptionRequete/*:Inscription/*:Verification")
def specificiteNode = requestDoc.createElementNS(namespace, 'Specificite')
inscriptionNode.insertBefore(specificiteNode, verificationNode)
def specCoefComptageNode = requestDoc.createElementNS(namespace, 'CoefficientComptage')
specificiteNode.appendChild(specCoefComptageNode)
def specCoefComptageText = requestDoc.createTextNode('1,0')
specCoefComptageNode.appendChild(specCoefComptageText)
def specEnfantPlaceNode = requestDoc.createElementNS(namespace, 'EnfantPlace')
specificiteNode.appendChild(specEnfantPlaceNode)
def specEnfantPlaceText = requestDoc.createTextNode('N')
specEnfantPlaceNode.appendChild(specEnfantPlaceText)
(…)
def specAbsence9DJNode = requestDoc.createElementNS(namespace, 'Absence9DemiJours')
specificiteNode.appendChild(specAbsence9DJNode)
def specAbsence9DJIndicateurNode = requestDoc.createElementNS(namespace, 'Indicateur')
specAbsence9DJNode.appendChild(specAbsence9DJIndicateurNode)
def specAbsence9DJIndicateurText = requestDoc.createTextNode('O')
specAbsence9DJIndicateurNode.appendChild(specAbsence9DJIndicateurText)
def specAbsence9DJDateNode = requestDoc.createElementNS(namespace, 'Date')
specAbsence9DJNode.appendChild(specAbsence9DJDateNode)
def specAbsence9DJDateText = requestDoc.createTextNode('2012-09-28')
specAbsence9DJDateNode.appendChild(specAbsence9DJDateText)
requestHolder.updateProperty(true)
De : Guillaume
Laforge [mailto:glaf...@codehaus.org]
Envoyé : lundi 15 avril 2013
15:00
À : Groovy User
Objet : Re: [groovy-user] How
to append a XML subtree to a given XML?