How to parse xml within CDATA inside XML host response?

Hi everyone,

I’ve read multiple resources here and elsewhere, but I failed to resolve my problem.

I send a POST request in XML to a host having CDATA made of another XML “request”, and the response has the same structure.
From the nested XML response inside CDATA I need to extract an attribute (‘ATTRIBUTE7’) that will be an environment variable.

My understanding is that due to CDATA the content of VASDataString is seen as a block of text that shall not be parsed.

How can I overtake this block?

Request:
Unfortunately I don’t manage to write here the content of CDATA so I can only paste it as image

Response:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Header>
    <HeadElem1>ABC123</HeadElem1>
    <HeadElem2>192</HeadElem2>
    <OriginalTime>2020-05-13T17:21:00.000+02:00</OriginalTime>
    <TransactionCode>70</TransactionCode>
    <ReqProcessing>false</ReqProcessing>
    <MessageType>false</MessageType>
    <MessageDirection>true</MessageDirection>
    <ResponseCode>0</ResponseCode>
</Header>
<Body>
    <VASProviderID>ABC</VASProviderID>
    <VASProductID>PIPPO</VASProductID>
    <VASMessageType>PIPPO_RESPONSE</VASMessageType>
    <TransactionTime>2020-05-13T17:21:00.000+02:00</TransactionTime>
    <RetailerID>0101</RetailerID>
    <BranchID>01</BranchID>
    <ShopID>0001</ShopID>
    <POSID>001</POSID>
    <VASDataString>&lt;HostMessage&gt;
            &lt;PIPPO_RESPONSE&gt;
                &lt;HostResponse Attr6="0000" ATTRIBUTE7="00039208052" Versione="2.0"&gt;
                    &lt;Header Attr8="XYZ" Attr9="0101010001001000192" Attr1="CD23" Timestamp="20200513184508520"/&gt;



                    &lt;Result Attr10="25838" ExDate="20220331" Attr11="000100031408"/&gt;
                &lt;/HostResponse&gt;
            &lt;/PIPPO_RESPONSE&gt;
        &lt;/HostMessage&gt;</VASDataString>
</Body>

In Tests there is:
var responseJson = xml2Json(responseBody);
console.log(responseJson);

and the Output in the console is:
{VASMessage: {…}}

  1. VASMessage: {…}
  2. $: {…}
    1. xmlns: “http://subdomain.domain.com
  3. Header: {…}
    HeadElem1: “SIA0101010001001”
    HeadElem2: “192”
    OriginalTime: “2020-05-13T17:21:00.000+02:00”
    TransactionCode: “70”
    ReqProcessing: “false”
    MessageType: “false”
    MessageDirection: “true”
    AeviResponseCode: “0”
  4. Body: {…}
    VASProviderID: “ABC”
    VASProductID: “PIPPO”
    VASMessageType: “PIPPO_RESPONSE”
    TransactionTime: “2020-05-13T17:21:00.000+02:00”
    RetailerID: “0101”
    BranchID: “01”
    ShopID: “0001”
    POSID: “001”
    VASDataString: "

Have you tried paring that part with xml2Json, something like:

xml2Json(responseBody.Body.VASDataString);

Hi @vdespa,

many thanks for help.
I’ve tried but it doesn’t resolve the problem.

Using
var responseJson = xml2Json(responseBody.Body.VASDataString);
console.log(responseJson);

the result is:
“TypeError: Cannot read property ‘VASDataString’ of undefined”

Thank you :wink:

Hi @alberto.bura,

I would use the xml2js module as xml2Json is included as part of the legacy sandbox API. Try something like this:

const xml2js = require('xml2js');
xml2js.parseString(pm.response.text(), (err, result) => {
    if (err) {
        console.error(err);
        return;
    }

    const body = result.VASMessage.Body[0].VASDataString[0];
    
    xml2js.parseString(body, (err, result) => {
        if (err) {
            console.error(err);
            return;
        }
        console.log(result);
    })
});

Related: XML response escape characters

Best,

Kevin

1 Like

I have presented a general idea on how to approach this. You need to make sure that the path to the property is correct.

Thank you so much!
It worked like charm.
I actually looked at XML response escape characters but it was beyond my knowledge.
Thank you again.

1 Like