Xml разбор изображений в флаттере - PullRequest
0 голосов
/ 13 апреля 2020

Я хочу разобрать изображение и использовать его в приложении. Но что мне действительно нужно, так это ссылка sr c, которая находится в теге изображения И чтобы получить CDATA в теге linik.

Это RSS-канал:

<rss version="2.0">
<channel>
<title>E! Online (US) - Top Stories</title>
<link>https://www.eonline.com/</link>
<category>(US) - Top Stories</category>
<description>
News from across the show-biz spectrum-TV, movies, music and celebrities
</description>
<language>en-us</language>
<ttl>10</ttl>
<pubDate>Mon, 13 Apr 2020 00:42:22 GMT</pubDate>
<lastBuildDate>Mon, 13 Apr 2020 00:42:22 GMT</lastBuildDate>
<image>
<title>E! Online (US) - Top Stories</title>
<width>120</width>
<height>60</height>
<link>https://www.eonline.com/</link>
<url>
https://images.eonline.com/eol_images/Entire_Site/2018223/rs_125x40-180323122205-EGoogleNews.png
</url>
</image>
<copyright>
© 2020 E! Entertainment Television, Inc. All rights reserved., https://www.eonline.com/about/copyright/index.jsp
</copyright>
<item>
<title>
Jennifer Lopez and Diddy Reunite on Instagram Live During His Dance-a-Thon Fundraiser
</title>
<link>
<![CDATA[
https://www.eonline.com/news/1140003/jennifer-lopez-and-diddy-reunite-on-instagram-live-during-his-dance-a-thon-fundraiser?cmpid=rss-000000-rssfeed-365-topstories&utm_source=eonline&utm_medium=rssfeeds&utm_campaign=rss_topstories
]]>
</link>
<description>
<img src="https://images.eonline.com/resize/66/66//eol_images/Entire_Site/201742//rs_600x600-170502090801-600-jennifer-lopez-diddy-met-gala-alex-rodriguez.jpg" height="66" width="66" border="0" alt="Jennifer Lopez, Sean Diddy Combs, MET Gala Then and Now" align="left" hspace="5" />Who's got better dance moves: Jennifer Lopez or Sean "Diddy" Combs? On Sunday, Diddy asked his fellow celeb friends to get up and dance for a cause. Posting on Instagram,...<br clear="all" />
</description>
<pubDate>Mon, 13 Apr 2020 00:42:22 GMT</pubDate>
<guid isPermaLink="false">b1140003</guid>
</item>
<item>
<title>
Tiger King Aftershow Episode: 5 of the Biggest Bombshells
</title>
<link>
<![CDATA[
https://www.eonline.com/news/1139997/tiger-king-aftershow-episode-5-of-the-biggest-bombshells?cmpid=rss-000000-rssfeed-365-topstories&utm_source=eonline&utm_medium=rssfeeds&utm_campaign=rss_topstories
]]>
</link>
<description>
<img src="https://images.eonline.com/resize/66/66//eol_images/Entire_Site/2020230//rs_600x600-200330065841-600-5-tiger-king-ch-033020.jpg" height="66" width="66" border="0" alt="Tiger King" align="left" hspace="5" />Hey all you cool cats and kittens... Netflix is giving the fans what they want: another Tiger King episode. That's right, the streaming service knew people were craving more...<br clear="all" />
</description>
<pubDate>Mon, 13 Apr 2020 00:04:21 GMT</pubDate>
<guid isPermaLink="false">b1139997</guid>
</item>
<item>
<title>
Mariah Carey Blesses Us on Easter With Virtual Performances of Her Emancipation of Mimi Singles
</title>
<link>
<![CDATA[
https://www.eonline.com/news/1139991/mariah-carey-blesses-us-on-easter-with-virtual-performances-of-her-emancipation-of-mimi-singles?cmpid=rss-000000-rssfeed-365-topstories&utm_source=eonline&utm_medium=rssfeeds&utm_campaign=rss_topstories
]]>
</link>
<description>
<img src="https://images.eonline.com/resize/66/66//eol_images/Entire_Site/2020312//rs_600x600-200412134740-600_Mariah_Carey_MP_4.12.20.jpg" height="66" width="66" border="0" alt="Mariah Carey, Instagram" align="left" hspace="5" />Mariah Carey is not only celebrating Easter Sunday this weekend but she's also celebrating the 15th anniversary of one of her most iconic albums, The Emancipation of Mimi. The...<br clear="all" />
</description>
<pubDate>Sun, 12 Apr 2020 22:27:45 GMT</pubDate>
<guid isPermaLink="false">b1139991</guid>
</item>
<item>
<title>
Hilary Duff Says Goodbye to Her Blonde Hair and Reveals an Epic New Look
</title>
<link>
<![CDATA[
https://www.eonline.com/news/1139985/hilary-duff-says-goodbye-to-her-blonde-hair-and-reveals-an-epic-new-look?cmpid=rss-000000-rssfeed-365-topstories&utm_source=eonline&utm_medium=rssfeeds&utm_campaign=rss_topstories
]]>
</link>
<description>
<img src="https://images.eonline.com/resize/66/66//eol_images/Entire_Site/2019913//rs_600x600-191013094707-600-hilary-duff.cm.101319.jpg" height="66" width="66" border="0" alt="Hilary Duff" align="left" hspace="5" />Hilary Duff's blonde hair is "so yesterday." The 32-year-old star is celebrating Easter 2020 with a new hairstyle and hair color. On Sunday afternoon, the Lizzie...<br clear="all" />
</description>
<pubDate>Sun, 12 Apr 2020 22:23:26 GMT</pubDate>
<guid isPermaLink="false">b1139985</guid>
</item>
<item>
<title>
The Disney Music Playlist You Need to Boost Your Mood Right Now
</title>
<link>
<![CDATA[
https://www.eonline.com/news/1138074/the-disney-music-playlist-you-need-to-boost-your-mood-right-now?cmpid=rss-000000-rssfeed-365-topstories&utm_source=eonline&utm_medium=rssfeeds&utm_campaign=rss_topstories
]]>
</link>
<description>
<img src="https://images.eonline.com/resize/66/66//eol_images/Entire_Site/202036//rs_600x600-200406183221-600.disney_playlist_e.gif" height="66" width="66" border="0" alt="Disney Song Playlist" align="left" hspace="5" />Get ready to start smiling! It's been 25 years since Disney released A Goofy Movie on April 7, 1995, spinning off the classic anthropomorphic dog Goofy and his teenage son Max Goof...<br clear="all" />
</description>
<pubDate>Sun, 12 Apr 2020 22:00:00 GMT</pubDate>
<guid isPermaLink="false">b1138074</guid>
</item>
<item>
<title>
Miley Cyrus Gives Cody Simpson a Makeover After Hair Transformation
</title>
<link>
<![CDATA[
https://www.eonline.com/news/1139960/miley-cyrus-gives-cody-simpson-a-makeover-after-hair-transformation?cmpid=rss-000000-rssfeed-365-topstories&utm_source=eonline&utm_medium=rssfeeds&utm_campaign=rss_topstories
]]>
</link>
<description>
<img src="https://images.eonline.com/resize/66/66//eol_images/Entire_Site/2020312//rs_600x600-200412134919-600-miley-cyrus-cody-simpson-makeover-2-instagram-041120.jpg" height="66" width="66" border="0" alt="Miley Cyrus, Cody Simpson, Makeover, Makeup, Instagram" align="left" hspace="5" />Sure you can give your boyfriend a "quarantine haircut," but how about a makeover? Miley Cyrus gave Cody Simpson one on Saturday night as the two practiced social distancing...<br clear="all" />
</description>
<pubDate>Sun, 12 Apr 2020 21:38:35 GMT</pubDate>
<guid isPermaLink="false">b1139960</guid>
</item>
<item>
<title>
Chrissy Teigen's Response to Simone Biles' Handstand Challenge Is All of Us
</title>
<link>
<![CDATA[
https://www.eonline.com/news/1139961/chrissy-teigen-s-response-to-simone-biles-handstand-challenge-is-all-of-us?cmpid=rss-000000-rssfeed-365-topstories&utm_source=eonline&utm_medium=rssfeeds&utm_campaign=rss_topstories
]]>
</link>
<description>
<img src="https://images.eonline.com/resize/66/66//eol_images/Entire_Site/2019913//rs_600x600-191013151453-600-simon-biles-medals-me-101319.jpg" height="66" width="66" border="0" alt="Simone Biles" align="left" hspace="5" />Simone Biles took the handstand challenge to a whole new level. While y'all have already seen stars like Jake Gyllenhaal, Tom Holland and Harrison Osterfield partake in the handstand...<br clear="all" />
</description>
<pubDate>Sun, 12 Apr 2020 20:22:24 GMT</pubDate>
<guid isPermaLink="false">b1139961</guid>
</item>
<item>
<title>
Michelle Money's Daughter Is ''Doing Well'' After Being Moved Out of the ICU
</title>
<link>
<![CDATA[
https://www.eonline.com/news/1139954/michelle-money-s-daughter-is-doing-well-after-being-moved-out-of-the-icu?cmpid=rss-000000-rssfeed-365-topstories&utm_source=eonline&utm_medium=rssfeeds&utm_campaign=rss_topstories
]]>
</link>
<description>
<img src="https://images.eonline.com/resize/66/66//eol_images/Entire_Site/202031//rs_600x600-200401065121-600-Michelle-Money-ch-040120.jpg" height="66" width="66" border="0" alt="Michelle Money" align="left" hspace="5" />Ryan and Michelle Money's daughter "had a big win" with her health. The former couple has been by Brielle's side following her "terrible skateboarding accident" in...<br clear="all" />
</description>
<pubDate>Sun, 12 Apr 2020 20:00:34 GMT</pubDate>
<guid isPermaLink="false">b1139954</guid>
</item>
<item>
<title>
Jessie Cast Shares Memories of Cameron Boyce During Zoom Reunion
</title>
<link>
<![CDATA[
https://www.eonline.com/news/1139936/jessie-cast-shares-memories-of-cameron-boyce-during-zoom-reunion?cmpid=rss-000000-rssfeed-365-topstories&utm_source=eonline&utm_medium=rssfeeds&utm_campaign=rss_topstories
]]>
</link>
<description>
<img src="https://images.eonline.com/resize/66/66//eol_images/Entire_Site/201967//rs_600x600-190707122820-600-cameron-boyce.cm.7719.jpg" height="66" width="66" border="0" alt="Cameron Boyce, Radio Disney Awards" align="left" hspace="5" />Debby Ryan and other former stars of the Disney Channel series Jessie recently reunited on Zoom as they practice social distancing amid the coronavirus pandemic. During the video chat,...<br clear="all" />
</description>
<pubDate>Sun, 12 Apr 2020 19:41:00 GMT</pubDate>
<guid isPermaLink="false">b1139936</guid>
</item>

Так что это RSS-канал, и это мой код:

import 'package:http/http.dart' as http;
import 'package:xml/xml.dart' as xml;

class Blogs {
  List<BlogModel> blogs = [];

  Future<void> getBlog() async {
    String url = 'http://syndication.eonline.com/syndication/feeds/rssfeeds/topstories.xml';

    var client = http.Client();
    var response = await client.get(url);
    var rssFeed = response.body;
    var raw = xml.parse(rssFeed);
    var elements = raw.findAllElements('item');

    elements.forEach((element) {
      BlogModel blogModel = BlogModel(
        title: element.findElements('title').first.text,
        url: element.findElements('link').first.text, //This is where I want to get the link in the CDATA
        urlToImage: element.findElements('description').first.text, //This is where I want to get the image src..
        description: element.findElements('description').first.text,
      );
      blogs.add(blogModel);
    });
  }
}

Но я не знаю, как получить доступ к строке http sr c в теге изображения.

Также мне нужно получить строку http в CDATA тега ссылки

1 Ответ

0 голосов
/ 13 апреля 2020

Этот код должен помочь:

elements.forEach((element) {

   // This will get the image's src.. since it is having problems 
   // with reading the <img>(it is considered as just text 
   // for some reason and not an xml tag).
   // I guess its due to a problem with the encoding of the rss feed
   // So reg-expression has to be used.

   final description = element.findElements('description').first.text;
   final urlRegExp = RegExp(r'(?:(?:https?):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
   final imgSrc = urlRegExp.firstMatch(description).group(0);

   // The rest of the parsing.
    BlogModel blogModel = BlogModel(
      title: element.findElements('title').first.text,
      url: element.findElements('link').first.text, //This will get the CDATA content with no problem
      urlToImage: imgSrc,
      description: element.findElements('description').first.text,
    );
    blogs.add(blogModel);
});
...