1

i am trying to read the token value from xml response body in javascript variable so far i tried

if (response.getStatusCode() != 200) {
api.fail("HTTP error: " + response.getStatusCode()); }
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName('string');

xml response ENTC~OTKtfKDRu7DIOj0buMfv+PdYDC62yS5GdRHeMO8H+/9UaUs8b2rpN67ONWO3XMkI96zV7x3jVQbeJNovp9Mhh4FbqXsqH2YB/MO/i8y7gJp2GG0QxczIC2oSosSSCAWK

Nikhil Patil
  • 11
  • 1
  • 3
  • What is not working? Do you get an error? From the code you posted you are never declaring xmlDoc. Maybe var x = xmlDoc.getElementsByTagName('string'); should be var x = xmlresponseBody.getElementsByTagName('string');? – Johan Apr 29 '20 at 08:05
  • changed from xmlresponse to xmlDoc – Nikhil Patil Apr 29 '20 at 08:16

1 Answers1

1

To read in JS, you need to use DOMParser API. Following is an example:

const text = "<string>This is my xml</string>"; //API response in XML
const parser = new DOMParser();
const xmlDOM = parser.parseFromString(text,"text/xml");
const value = xmlDOM.getElementsByTagName("string")[0].childNodes[0].nodeValue;
console.log(value)

Edit: Example using fetch() API

fetch('Your_API_URL')
.then(response=>response.text())
.then(data=>{
    const parser = new DOMParser();
    const xmlDOM = parser.parseFromString(data,"text/xml");
    const value = xmlDOM.getElementsByTagName("string")[0].childNodes[0].nodeValue;
    console.log(value)
})
.catch(err=>console.log(err))
Pramod Mali
  • 1,588
  • 1
  • 17
  • 29