@jekanok

Как парсить файл xml в js?

Привет вопрос в том что, у меня есть файл xml (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>
....
, который хочется распарсить в формат json или как с ним нормально работать

вот так я обращаюсь к нему
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]);
  },

Спасибо)
  • Вопрос задан
  • 88 просмотров
Пригласить эксперта
Ответы на вопрос 1
yarkov
@yarkov Куратор тега Vue.js
Помог ответ? Отметь решением.
Как насчёт загуглить по запросу xml2json? Первая же ссылка.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы