To retrieve data from XML file using JavaScript,
fetch
the file from a server, then parse it with DOMParser
.Here's how you do it:
fetch('https://whaa.dev/sitemap.xml')
.then(response => response.text())
.then(xml => new DOMParser().parseFromString(xml, 'text/xml'))
.then(xmlDom => {
// DOMParser turns XML into DOM tree,
// so you can use regular DOM methods like querySelector
const firstUrlEntry = xmlDom.querySelector('url loc').textContent;
// https://whaa.dev/
console.log(firstUrlEntry);
});