# Parsing XML variables to enivornment

**URL:** <https://community.postman.com/t/parsing-xml-variables-to-enivornment/38006>\
**Category:** Help Hub\
**Tags:** tests, parse, xml\
**Created:** [August 9, 2022, 2:27am UTC](https://community.postman.com/t/parsing-xml-variables-to-enivornment/38006 "2022-08-09T02:27:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambowering](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@adambowering](https://community.postman.com/u/adambowering)\
**Post date:** [August 9, 2022, 2:27am UTC](https://community.postman.com/t/parsing-xml-variables-to-enivornment/38006/1 "2022-08-09T02:27:20Z")

</div>

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:

```auto
``<?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. ▶$: {…}

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?

---

<div class="post-metadata">

**Author:** ![adambowering](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@adambowering](https://community.postman.com/u/adambowering)\
**Post date:** [August 9, 2022, 6:16am UTC](https://community.postman.com/t/parsing-xml-variables-to-enivornment/38006/2 "2022-08-09T06:16:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [August 9, 2022, 9:27am UTC](https://community.postman.com/t/parsing-xml-variables-to-enivornment/38006/3 "2022-08-09T09:27:27Z")

</div>

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;

```auto
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]);
    }
}

```

---

<div class="post-metadata">

**Author:** ![adambowering](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@adambowering](https://community.postman.com/u/adambowering)\
**Post date:** [August 10, 2022, 1:19am UTC](https://community.postman.com/t/parsing-xml-variables-to-enivornment/38006/4 "2022-08-10T01:19:34Z")

</div>

> [@w4dd325](#):
>
> `const body = responseBody;`

Thanks for the response w4dd325

I have tried:

```auto
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!

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [August 10, 2022, 8:19am UTC](https://community.postman.com/t/parsing-xml-variables-to-enivornment/38006/5 "2022-08-10T08:19:56Z")

</div>

Try;

```auto
const body = pm.response.text();

```
