mCY
mCY

Reputation: 2821

groovy XmlSlurper parse XML

I get a XML file like this:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:udb="http://somenamespace>
    <soap:Header/>
    <soap:Body>
        <udb:ProvideUDBIdentityInformationRequest>
            <udb:RequestID>1</udb:RequestID>
            <udb:IDnumber>1</udb:IDnumber>
            <udb:UnifiedNumber>3</udb:UnifiedNumber>
        </udb:ProvideUDBIdentityInformationRequest>
    </soap:Body>
</soap:Envelope> 

I want to save the below string to another file.

    <soap:Body>
        <udb:ProvideUDBIdentityInformationRequest>
            <udb:RequestID>1</udb:RequestID>
            <udb:IDnumber>1</udb:IDnumber>
            <udb:UnifiedNumber>3</udb:UnifiedNumber>
        </udb:ProvideUDBIdentityInformationRequest>
    </soap:Body>

How can I achieve that ? In fact, I've refer to some tutorial like this one ,http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper, but cannot find such a method in XmlSlurper.

Thanks in advance!

Upvotes: 0

Views: 1830

Answers (1)

tim_yates
tim_yates

Reputation: 171054

Assuming you have the original xml in a variable called xml, you can do:

String output = new groovy.xml.StreamingMarkupBuilder().bind {
    mkp.yield new XmlSlurper().parseText( xml ).Body
}

Then, output equals:

<soap:Body xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>
    <udb:ProvideUDBIdentityInformationRequest xmlns:udb='http://somenamespace'>
        <udb:RequestID>1</udb:RequestID>
        <udb:IDnumber>1</udb:IDnumber>
        <udb:UnifiedNumber>3</udb:UnifiedNumber>
    </udb:ProvideUDBIdentityInformationRequest>
</soap:Body>

Upvotes: 2

Related Questions