Skip to content Skip to sidebar Skip to footer

How To Read String From Xml Response Body In Javascript

i am trying to read the token value from xml response body in javascript variable so far i tried xml response ENTC~OTKtfKDRu7DIOj0buMfv+PdYDC62yS5GdRHeMO8H+/9UaUs8b2rpN67ONWO3XM

Solution 1:

Yes, all DOM API functions works good in the XML, but i recommend you to look towards this approach. To use this syntax you should use xmldom and xpath libraries;

Solution 2:

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 XMLconst parser = newDOMParser();
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 = newDOMParser();
    const xmlDOM = parser.parseFromString(data,"text/xml");
    const value = xmlDOM.getElementsByTagName("string")[0].childNodes[0].nodeValue;
    console.log(value)
})
.catch(err=>console.log(err))

Post a Comment for "How To Read String From Xml Response Body In Javascript"