jadjoubran / codetogo.io

🚀 JavaScript code to go - Find updated snippets for common JavaScript use cases
https://codetogo.io
MIT License
231 stars 30 forks source link

Use Case Suggestion: Fetch XML #235

Closed adamjohnson closed 3 years ago

adamjohnson commented 4 years ago

Fetch JSON exists, but consider adding fetching XML (for those poor souls that don't have access to JSON). Here's a code snippet:

fetch('http://xml-coreutils.sourceforge.net/food.xml')
  .then(response => response.text())
  .then(data => {
    // console.log(data); // The raw string
    let parser = new DOMParser();
    let xml = parser.parseFromString(data, 'application/xml');
    console.log(xml);
  })
  .catch(console.error);

Here's a YouTube video of Steve Griffith working with JS to fetch XML.

Love Code To Go by the way! Thanks for all you do.

jadjoubran commented 3 years ago

Thanks for the use case! And I'm really happy to hear that you love CodeToGo!

I've added https://codetogo.io/api/users.xml as well as the use case: How to fetch XML in JavaScript