Search code examples
textgroovysubstringmtom

GROOVY with MTOM - how to cut XML


I am trying to cut an mtom soap xml response to retrieve only the real response from the overall response.

Plain text:

------=_Part_406790_188859372.1611576835975
Content-Type: application/xop+xml;charset=UTF-8;type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: <67b0f124-2a07-4c56-a6f2-7db282ad7f65>

<?xml version="1.0" encoding="UTF-8" ?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Header/><env:Body>><ns2:Service IdcService="GET_FILE"></env:Body></env:Envelope>
------=_Part_406790_188859372.1611576835975
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <582442cb-3d80-4eaa-b723-0ef901d11c4f>

<?xml version="1.0" encoding="UTF-8" ?> <DATA_DS><G_1><PAYROLL_ACTION_ID>4119</PAYROLL_ACTION_ID><G_2><FILE_FRAGMENT><Feed_V7><REP_CATEGORY_NAME>Blubblub</REP_CATEGORY_NAME><parameters><request_id>234234</request_id><FLOW_NAME>Lalelu 25-01-2021 10:49:33 | Blub</FLOW_NAME><legislative_data_group_id/><effective_date>2021-01-01</effective_date><start_date/><report_category_id>21321</report_category_id><action_parameter_group_id/><changes_only>N</changes_only></parameters></Person></Feed_V7></FILE_FRAGMENT></G_2></G_1></DATA_DS>
------=_Part_406790_188859372.1611576835975--

This is what I want:

<?xml version="1.0" encoding="UTF-8" ?><DATA_DS>.....all between.....</DATA_DS>

Groovy trial via substrings:

def bodySize = body.length()
def String subString = body.toString().substring(566)
def otherString = subString.substring(0,76)
println(otherString)

This is really bad practice, but I am running out of ideas how to retrive the xml fully dynamically.

Thanks for input (even only for methods - not asking for finished code rather which methods to use to find the right section in the response).


Solution

  • off the hip:

    String txt = '''
    ------=_Part_406790_188859372.1611576835975
    Content-Type: application/xop+xml;charset=UTF-8;type="text/xml"
    Content-Transfer-Encoding: 8bit
    Content-ID: <67b0f124-2a07-4c56-a6f2-7db282ad7f65>
    
    <?xml version="1.0" encoding="UTF-8" ?>
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Header/><env:Body>><ns2:Service IdcService="GET_FILE"></env:Body></env:Envelope>
    ------=_Part_406790_188859372.1611576835975
    Content-Type: application/octet-stream
    Content-Transfer-Encoding: binary
    Content-ID: <582442cb-3d80-4eaa-b723-0ef901d11c4f>
    
    <?xml version="1.0" encoding="UTF-8" ?><DATA_DS><G_1>4119</G_1></DATA_DS>
    ------=_Part_406790_188859372.1611576835975--'''
    
    def matches = ( txt =~ /<\?xml.*><DATA_DS>.*<\/DATA_DS>/ ).findAll()
    String match = matches ? matches.first() : null
    assert match == '<?xml version="1.0" encoding="UTF-8" ?><DATA_DS><G_1>4119</G_1></DATA_DS>'