Parsing XML variables to enivornment

Hi Everyone!
Quite new to postman, but have managed to created a few basic APIā€™s and looking at my first real integration project.
I have an API spitting out a response and I am trying to extract the ā€˜WorkOrderNumberā€™ in the following response:

``<?xml version="1.0" encoding="utf-8"?>
``<feed xml:base="http://mex.viterra.com.au/MEXData/OData.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <id>http://mex.viterra.com.au/MEXData/OData.svc/WorkOrders</id>
    <title type="text">WorkOrders</title>
    <updated>2022-08-09T02:19:57Z</updated>
    <link rel="self" title="WorkOrders" href="WorkOrders" />
    <entry>
        <id>http://mex.viterra.com.au/MEXData/OData.svc/WorkOrders(134031)</id>
        <category term="MEXModel.WorkOrder" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" title="WorkOrder" href="WorkOrders(134031)" />
        <title />
        <updated>2022-08-09T02:19:57Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:WorkOrderID m:type="Edm.Int32">134031</d:WorkOrderID>
                <d:WorkOrderNumber m:type="Edm.Int32">131401</d:WorkOrderNumber>
            </m:properties>
        </content>
    </entry>
</feed>
``

In my Test Script I have tried the following:
``
var responseJson = xml2Json(responseBody);
var WorkOrderNumber = responseJson['feed']['entry']['content']['m:properties']['d:WorkOrderNumber'];
pm.environment.set("LastWONumber", WorkOrderNumber);

The response has what I need, the number but with additional text:
{_: ā€œ131401ā€, $: {ā€¦}}

  1. _: ā€œ131401ā€

  2. :arrow_forward:$: {ā€¦}

  3. m:type: ā€œEdm.Int32ā€

I just want the number 131401 so I can pass to a variable to LastWONumber but it appears to pass all that information, not just the number.

Any hep please?

For anyone looking into a similar solution, I did find a slightly imperfect one.
I have sliced the text to return just the number. Not ideal if anything ever changes, but a work around unless anyone else has a better solution.

let temp = pm.response.text(), WorkOrderNumber = temp.slice(temp.indexOf('d:WorkOrderNumber m:type="Edm.Int32">') + 37, -60); pm.environment.set("LastWONumber", WorkOrderNumber);

Hi @adambowering

You could use RegEx to get the value using the text as a left and right boundary.

Here is an example of one I used before;

const body = responseBody;
if(body!=null && body.length>0){
    let array = new RegExp("secret\=(.*?)\"").exec(body);
    if(array!=null && array.length>0 && array[1]!=null){
        //pm.globals.set("secret", ""+array[1]);
        console.log(array[1]);
    }
}

Thanks for the response w4dd325

I have tried:

const body = xml2Json(responseBody);

if(body!=null && body.length>0){
    let array = new RegExp('WorkOrderNumber').exec(body);
    if(array!=null && array.length>0 && array[1]!=null){
        console.log(array[1]);
        console.log('Worked')
    }
}

However it doesnā€™t seem to trigger the if statement. Looks like the body.length isnā€™t > 0
Have run console.log(body.length) but comes back as undefined.

Tried converting the xml to Json to get the array, so iā€™m a bit stumped!

Try;

const body = pm.response.text();