Forum Discussion
praneeth12
5 years agoOccasional Contributor
Expected result
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
soap:actor="https://www.w3schools.com/code/">234
</m:Trans>
</soap:Header>
<soap:Body>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<PRICE>10.90</PRICE>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<PRICE>9.90</PRICE>
</CD>
</CATALOG>
</soap:Body>
</soap:Envelope>
Below is requst
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
soap:actor="https://www.w3schools.com/code/">234
</m:Trans>
</soap:Header>
<soap:Body>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<PRICE>10.90</PRICE>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<PRICE>9.90</PRICE>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<PRICE>9.90</PRICE>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary Moore</ARTIST>
<PRICE>10.20</PRICE>
</CD>
</CATALOG>
</soap:Body>
</soap:Envelope>
- appliedMagic5 years agoOccasional Contributor
Hi,
maybe this will help you to get build/append your requests. Since I do not know where you get the data from and how it is provided (represented in the code) I will assume a list of maps, the map contains the CD information.
//the request body with no CDs def reqBody = """<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> <soap:Header> <m:Trans xmlns:m="https://www.w3schools.com/transaction/" soap:actor="https://www.w3schools.com/code/">234</m:Trans> </soap:Header> <soap:Body> <CATALOG/> </soap:Body> </soap:Envelope>""" //for our test lets use a list of maps, each map contains one CD def cds = [ [ "TITLE": "Empire Burlesque", "ARTIST": "Bob Dylan", "PRICE": "10.90"], [ "TITLE": "Hide your heart", "ARTIST": "Bonnie Tyler", "PRICE": "9.90"], [ "TITLE": "Greatest Hits", "ARTIST": "Dolly Parton", "PRICE": "9.90"], [ "TITLE": "Still got the blues", "ARTIST": "Gary Moore", "PRICE": "10.20"] ] //read in the request body and find the catalog node def root = new XmlParser().parseText( reqBody ) //<Envelope> def catalog = root.'**'.find { it.name().toString().equalsIgnoreCase("catalog") } //find catalog //now add all CDs for(cd in cds) { //create CD node Node cdNode = new Node(catalog, "CD") //add information to CD node for(key in ["TITLE", "ARTIST", "PRICE"]) { cdNode.appendNode(key, cd[key]) } } //check result till here log.info(groovy.xml.XmlUtil.serialize(root)) //add even more CDs def moreCDs = [ [ "TITLE": "Greatest Hits Vol.1", "ARTIST": "Various Artists", "PRICE": "14.95"], [ "TITLE": "Greatest Hits Vol.2", "ARTIST": "Various Artists", "PRICE": "17.95"] ] // add even more CDs for(cd in moreCDs) { //create CD node Node cdNode = new Node(catalog, "CD") //add information to CD node for(key in ["TITLE", "ARTIST", "PRICE"]) { cdNode.appendNode(key, cd[key]) } } //convert to text def requestXML = groovy.xml.XmlUtil.serialize(root) //check result log.info(requestXML) log.info("done")
I used this to lookup a few examples of how to interact with an XML using XMLParser (didn't really (often) use it before):
(documentation)
http://docs.groovy-lang.org/latest/html/api/groovy/util/Node.html
(some examples of how to use them)
https://www.codota.com/web/assistant/code/rs/5c6574441095a5000151d7bf#L241
We started with:
<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> <soap:Header> <m:Trans xmlns:m="https://www.w3schools.com/transaction/" soap:actor="https://www.w3schools.com/code/">234</m:Trans> </soap:Header> <soap:Body> <CATALOG/> </soap:Body> </soap:Envelope>
The result, after we run the code, looks like this:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> <soap:Header> <m:Trans xmlns:m="https://www.w3schools.com/transaction/" soap:actor="https://www.w3schools.com/code/">234</m:Trans> </soap:Header> <soap:Body> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <PRICE>10.90</PRICE> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <PRICE>9.90</PRICE> </CD> <CD> <TITLE>Greatest Hits</TITLE> <ARTIST>Dolly Parton</ARTIST> <PRICE>9.90</PRICE> </CD> <CD> <TITLE>Still got the blues</TITLE> <ARTIST>Gary Moore</ARTIST> <PRICE>10.20</PRICE> </CD> <CD> <TITLE>Greatest Hits Vol.1</TITLE> <ARTIST>Various Artists</ARTIST> <PRICE>14.95</PRICE> </CD> <CD> <TITLE>Greatest Hits Vol.2</TITLE> <ARTIST>Various Artists</ARTIST> <PRICE>17.95</PRICE> </CD> </CATALOG> </soap:Body> </soap:Envelope>
Related Content
- 3 years ago
Recent Discussions
- 15 years ago