Peter
Peter

Reputation: 1021

Get just content of soap response in Groovy

I have following soap response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
     <GetHTMLResponse xmlns="http://www.webserviceX.NET">
       <GetHTMLResult>
         TEST
       </GetHTMLResult>
     </GetHTMLResponse>
   </soap:Body>
</soap:Envelope>

Now I want a method which deliverys me this xml:

<root>
  <GetHTMLResponse xmlns="http://www.webserviceX.NET">
    <GetHTMLResult>
        TEST
    </GetHTMLResult>
  </GetHTMLResponse>
</root>

So Soap envelope and body should be removed and everything in Soap Body should be under the tag root. How can I make this?

Upvotes: 2

Views: 1489

Answers (1)

tim_yates
tim_yates

Reputation: 171054

SO you could do this:

import groovy.xml.*

def xml = '''<?xml version="1.0" encoding="utf-8"?>
            |<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            |   <soap:Body>
            |     <GetHTMLResponse xmlns="http://www.webserviceX.NET">
            |       <GetHTMLResult>
            |         TEST
            |       </GetHTMLResult>
            |     </GetHTMLResponse>
            |   </soap:Body>
            |</soap:Envelope>'''.stripMargin()


def result = XmlUtil.serialize(new StreamingMarkupBuilder().bind {
    root { 
        mkp.yield new XmlSlurper(false, false).parseText(xml).'soap:Body'.GetHTMLResponse
    }
})

println result

which outputs:

<?xml version="1.0" encoding="UTF-8"?><root>
  <GetHTMLResponse xmlns="http://www.webserviceX.NET">
    <GetHTMLResult>
         TEST
       </GetHTMLResult>
  </GetHTMLResponse>
</root>

Upvotes: 1

Related Questions