J
J
jekanok2021-02-25 20:52:30
Vue.js
jekanok, 2021-02-25 20:52:30

How to parse xml file in js?

Hello, the question is, I have an xml file (rss)

<channel>
<title>Yahoo News - Latest News & Headlines</title>
<link>https://www.yahoo.com/news</link>
<description>The latest news and headlines from Yahoo! News. Get breaking news stories and in-depth coverage with videos and photos.</description>
<language>en-US</language>
<copyright>Copyright (c) 2021 Yahoo! Inc. All rights reserved</copyright>
<pubDate>Thu, 25 Feb 2021 06:06:49 -0500</pubDate>
<ttl>5</ttl>
<image>
<title>Yahoo News - Latest News & Headlines</title>
<link>https://www.yahoo.com/news</link>
<url>http://l.yimg.com/rz/d/yahoo_news_en-US_s_f_p_168x21_news.png</url>
</image>
<item>
<title>Mitch McConnell: Nancy Pelosi's plan for investigating the Capitol attack is a 'bizarre partisan concept'</title>
<link>https://news.yahoo.com/mc-connell-pelosi-capitol-attack-commission-224338603.html</link>
<pubDate>2021-02-24T22:43:38Z</pubDate>
<source url="https://news.yahoo.com/">Yahoo News</source>
<guid isPermaLink="false">mc-connell-pelosi-capitol-attack-commission-224338603.html</guid>
<media:content height="86" url="https://s.yimg.com/os/creatr-uploaded-images/2021-02/bb655220-76e5-11eb-9f62-3bb7aaae47a7" width="130"/>
<media:credit role="publishing company"/>
</item>
....
, which I want to parse into json format or how to work with it normally

, this is how I access it
const xmlFile = await fetch("rss.xml")
      .then((response) => response.text())
      .then((data) => {
        const parser = new DOMParser();
        const xml = parser.parseFromString(data, "application/xml");
        return xml.getElementsByTagName("item");
      })

      .catch(console.error);

    // console.log(xmlFile.length);

    console.log(xmlFile[0]);
  },

Thank you)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2021-02-25
@yarkov Vue.js

How about googling for xml2json? The first link.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question