How to fetch XML in JavaScript

JSON
By Jad Joubran · 
Last updated Feb 26, 2024
fetch("https://codetogo.io/api/users.xml")
  .then(response => response.text())
  .then(data => {
    const parser = new DOMParser();
    const xml = parser.parseFromString(data, "application/xml");
    console.log(xml);
  })
  .catch(console.error);
<users>
    <user>
        <id>1</id>
        <name>Alex</name>
    </user>
    <user>
        <id>2</id>
        <name>Sam</name>
    </user>
</users>
Fetch API use cases on Learn JavaScript