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="" xmlns="" xmlns:d="" xmlns:m="">
    <title type="text">WorkOrders</title>
    <link rel="self" title="WorkOrders" href="WorkOrders" />
        <category term="MEXModel.WorkOrder" scheme="" />
        <link rel="edit" title="WorkOrder" href="WorkOrders(134031)" />
        <title />
            <name />
        <content type="application/xml">
                <d:WorkOrderID m:type="Edm.Int32">134031</d:WorkOrderID>
                <d:WorkOrderNumber m:type="Edm.Int32">131401</d:WorkOrderNumber>

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]);

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){

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!


const body = pm.response.text();