Parse XML response to get a value

Hi guys,

I have a POST request and get the following response in XML:

<feed  xmlns="http://www.w3.org/2005/Atom">
    <link href="https://xyz?page=1&pagesize=50" rel="self" />
    <link href="https://xyz/biprws/v1/cmsquery?page=1&pagesize=50" rel="first" />
    <link href="https://xyz/biprws/v1/cmsquery?page=1&pagesize=50" rel="last" />
    <entry>
        <content type="application/xml">
            <attrs xmlns="http://www.sap.com/rws/bip">
                <attr name="SI_ID" type="integer">302155</attr>
                <attr name="SI_NAME" type="string">TEXT</attr>
            </attrs>
        </content>
    </entry>
</feed>

I would like to parse this response (in Tests script) to get the “SI_ID” value 302155:

console.log(xml2Json(pm.response.text()));

I get “Undefined” error message.

Thanks,
Tibor

Hi @tibornagy

I was playing around with this and noticed it doesn’t like parsing the href links as XML. If you remove them it works.

Are you in control of the response body? … If so you could change it to this;

<feed xmlns="http://www.w3.org/2005/Atom">
    <![CDATA[
        <link href="https://xyz?page=1&pagesize=50" rel="self" />
        <link href="https://xyz/biprws/v1/cmsquery?page=1&pagesize=50" rel="first" />
        <link href="https://xyz/biprws/v1/cmsquery?page=1&pagesize=50" rel="last" />
    ]]>
    <entry>
        <content type="application/xml">
            <attrs xmlns="http://www.sap.com/rws/bip">
                <attr name="SI_ID" type="integer">302155</attr>
                <attr name="SI_NAME" type="string">TEXT</attr>
            </attrs>
        </content>
    </entry>
</feed>

If you are not in control of the response body, you could parse as text and use a RegEx to pull out the value you want, like this;

const response = pm.response.text();
console.log(response);

let formatMatch = new RegExp("<attr name=\"SI_ID\" type=\"integer\">(.*?)</attr>").exec(response);

pm.test("Validate id value", function () {
    pm.expect(formatMatch, "Format does not match regular expression.").to.not.eql(null);
    console.log("formatMatch = " + formatMatch[1]);
});

Yes it looks like that I cant parse the response body because of those href links. I cant control the response body. The RegExp is a good idea but would be easier the xml2Json stuff.
Thanks for the answer