Skip to content

Instantly share code, notes, and snippets.

@upalexgill
Created October 27, 2023 16:32
Show Gist options
  • Select an option

  • Save upalexgill/12f6bbd80fe635546b3f0f9818695665 to your computer and use it in GitHub Desktop.

Select an option

Save upalexgill/12f6bbd80fe635546b3f0f9818695665 to your computer and use it in GitHub Desktop.
Parse XML data to string array
const xmlData = ``;
function getCommaSeparatedUrls(xml) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xml, 'text/xml');
const sitemapNodes = xmlDoc.getElementsByTagName('loc');
const urls = [];
for (let i = 0; i < sitemapNodes.length; i++) {
urls.push(sitemapNodes[i].textContent);
}
return urls.join(',');
}
const commaSeparatedUrls = getCommaSeparatedUrls(xmlData);
const urlArray = commaSeparatedUrls.split(',');
console.log(urlArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment