# How to parse multiple xml objects present at same level in the response

**URL:** <https://community.postman.com/t/how-to-parse-multiple-xml-objects-present-at-same-level-in-the-response/25350>\
**Category:** Help Hub\
**Tags:** xml-parsing\
**Created:** [June 30, 2021, 12:08pm UTC](https://community.postman.com/t/how-to-parse-multiple-xml-objects-present-at-same-level-in-the-response/25350 "2021-06-30T12:08:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaibhavbarmkar](https://avatars.discourse-cdn.com/v4/letter/v/ecd19e/32.png) [@vaibhavbarmkar](https://community.postman.com/u/vaibhavbarmkar)\
**Post date:** [June 30, 2021, 12:08pm UTC](https://community.postman.com/t/how-to-parse-multiple-xml-objects-present-at-same-level-in-the-response/25350/1 "2021-06-30T12:08:12Z")

</div>

_My question_:  
I’m getting the below XML response from my API call

```auto
<data name="xyz" isRequired="Yes" departmentNumber="123"> </data>
<data name="abc" isRequired="No" departmentNumber="125"> </data>
<data name="mnq" isRequired="Yes" departmentNumber="123"> </data>

```

when I’m doing let jsonObject = xml2Json(pm.response.text()); its only returning me first data object. How I can get all the data objects and parse them to JSON as array?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [June 30, 2021, 1:24pm UTC](https://community.postman.com/t/how-to-parse-multiple-xml-objects-present-at-same-level-in-the-response/25350/2 "2021-06-30T13:24:52Z")

</div>

Hello @vaibhavbarmkar 👋

Can you please have a look at this [post](https://community.postman.com/t/how-to-retrieve-data-from-odata-response/11025)?

Try using

```auto

let xml2js = require('xml2js');

let xml = pm.response.text();

```

---

<div class="post-metadata">

**Author:** ![neilstudd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/neilstudd/32/8213_2.png) [@neilstudd](https://community.postman.com/u/neilstudd)\
**Post date:** [June 30, 2021, 1:50pm UTC](https://community.postman.com/t/how-to-parse-multiple-xml-objects-present-at-same-level-in-the-response/25350/3 "2021-06-30T13:50:20Z")

</div>

Hi @vaibhavbarmkar,

Is this the entire response of your API call? If so, I can replicate the problem, and I understand why it is happening; XML documents are supposed to only have a single root node, and this is why `xml2json` only parses the first object that it encounters. (I see the same problem when I try to use `xml2js` as well.)

I think the best way to resolve would be to pad your response to include a parent tag, for instance:

```auto
jsonObject = xml2Json("<myData>" + pm.response.text() + "</myData>");
console.log(jsonObject); 

```

Now I see all three nested items are correctly parsed:

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/7/797a4963c0ebe6bcf8ba112b720f8160342be270.png)

You can then access individual items, e.g.

```auto
console.log(jsonObject.myData.data[0].$.name)

```

---

<div class="post-metadata">

**Author:** ![vaibhavbarmkar](https://avatars.discourse-cdn.com/v4/letter/v/ecd19e/32.png) [@vaibhavbarmkar](https://community.postman.com/u/vaibhavbarmkar)\
**Post date:** [June 30, 2021, 2:19pm UTC](https://community.postman.com/t/how-to-parse-multiple-xml-objects-present-at-same-level-in-the-response/25350/4 "2021-06-30T14:19:06Z")

</div>

Thanks, @neilstudd for the above solution. As of now, I used cheerio lib for parsing

const $ = cheerio.load(jsonData1, {  
xml: {  
normalizeWhitespace: true,  
},  
});

its bit complex but worked for me. But I think your solution should be cleaner approach.
