Parsing XML with same hierarchy name in the responseBody

Hello community,

Another newbie question:
I have to parse and get values of specific key which repeats itself in the responseBody.

Snippet of response data:

<collection xmlns:y=“http://tail-f.com/ns/rest”>
<analytics-cluster xmlns=“http://versa-networks.com/vnms/nms”>
<name>Clus2
</analytics-cluster>
<analytics-cluster xmlns=“http://versa-networks.com/vnms/nms”>
<name>suraj-va1
</analytics-cluster>
</collection>

As you can see, the output has Analytics-Cluster listed twice and in such cases, I want to be able to parse them as a list of clusters. If I do normal xml to json, I do not get the value of either one. How do I parse them and get them as a list? In other words, I want to get the values “Clus2” and “suraj-va1” into a list variable.

Thanks,
Suraj

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Hi @surajc,

This doesn’t look like valid XML since the <name> tag is never closed. I’m not sure what you’re using to generate it but when closing the tag and running the result through XML to JSON converters I’m getting a list of names as expected.
This is the XML I’m using:

<collection xmlns:y="http://tail-f.com/ns/rest">
<analytics-cluster xmlns="http://versa-networks.com/vnms/nms">
<name>Clus2</name>
</analytics-cluster>
<analytics-cluster xmlns="http://versa-networks.com/vnms/nms">
<name>suraj-va1</name>
</analytics-cluster>
</collection>

Hi @arlemi

Thanks for the response. The Tags were infact closed, but when I copied and pasted it in the support page here, I escaped the first one and not the second one.

Can you provide me a sample test script to get the list of all the names though? For me, when there is single one, I have the variable for the name populated using the test script, but when there are more than one entries in the cluster (resulting in two names in the cluster), I get a blank value assigned.

Thanks again,
Suraj

Hi @surajc,

I’ve created a collection showing how you can generate a list of names based of the XML you shared here. You can import it in Postman by clicking Import -> Import From Link and pasting this: https://www.getpostman.com/collections/d388a2f1c90795155bc6

I’m using the xml2Json that is available by default and then looping through the JSON object to get each name and push it to an array.

Let me know if this works for you!

Hi @surajc,

Great question. So you can also do this by using Cheerio.js library. Its power is in parsing markup languages such as HTML and XML.

in order to get the values out, I would use the following code:

const $ = cheerio.load(responseBody, {
ignoreWhitespace: true,
xmlMode: true
  });

console.log($('collection').children('analytics-cluster').text())

If you want get a good example for working with XML Services in Postman, I’d recommend watching this video I made :slight_smile:

You can find a sample of more Cheerio.js code that I used to generate the result above.

I hope this helps!

Orest

Thanks much, @arlemi. That worked. Was able to replicate it for other such use cases across my other test scripts.

1 Like

Thanks, @odanylewycz. Will try it out sometime.

1 Like