官术网_书友最值得收藏!

XML feed generation

For blog reader's search and syndication, a blog needs to produce an RSS feed. RSS is an XML specification that lists blog posts in a specified format as separate items that are simple to process.

Note

In late 1990s, there were many attempts to create a syndication format for the Web. Dan Libby and Ramanathan V. Guha from Netscape created the RDF site summary format in 1999 for use in the My.Netscape.Com portal. The RDF specification was refined and eventually became RSS.

The competing (and in my opinion, much superior!) feed format is Atom. Atom was developed to fix the shortcomings of RSS has not gained much traction with simple RSS and web services used for more advanced applications.

As well as blog and news publications, RSS is also established as the standard format for podcasting and its supporting clients and tools.

The full specification is available at https://validator.w3.org/feed/docs/rss2.html.

To produce the feed for the blog, posts can easily be iterated as we already have a list of IDs. To produce a valid XML feed, the xml package will be used. This is not part of the SDK and is an open source library, which has become a favorite for XML creation and processing in Dart. There was at one time an XML processing package in the SDK, but the xml package was improved so quickly that it made more sense to make it the standard package. Let's have a look at the following code snippet for more information:

  String getRSSFeed() {
    var RssXb = new XmlBuilder();
    RssXb.processing('xml', 'version="1.0"');

    RssXb.element('rss', attributes: {'version': '1.0'}, nest: () {
      RssXb.element('channel', nest: () {
        IDs.forEach((int postID) {
          BlogPost post = getBlogPost(postID);
          RssXb.element('item', nest: () {
            RssXb.element('pubDate', nest: () {
              RssXb.text(post._date);
            });
            RssXb.element('title', nest: () {
              RssXb.text(post._title);
            });
            RssXb.element('link', nest: () {
              RssXb.text("http://127.0.0.1:8080/post${post._id}.html");
            });
          }); //item
        });
      }); //channel
    }); //rss

    var xml = RssXb.build();
    return xml.toXmlString(pretty: true);
  }

Elements are added in a nested fashion here, which reflects the RSS structure. Once the data structure is put together, the build method constructs the XML structure, and finally, it is converted into a string.

Tip

For simple XML, it is tempting just to concatenate strings. However, an XML library can provide facilities such as character encoding, validation, and formatting ,which are well worth the slightly more verbose code on the smaller use cases.

As it will be viewed by human beings as well as computers, the optional name parameter option pretty is set to true for a neatly formatted output, as shown in the following example:

<?xml version="1.0"?>
<rss version="1.0">
  <channel>
    <item>
      <pubDate>02/05/2015</pubDate>
      <title>Lemur Facts</title>
      <link>http://127.0.0.1:8080/post6.html</link>
    </item>
...

Note

The RSS feed is served to http://127.0.0.1:8080/feed.xml and can be viewed directly in most web browsers.

Serving the RSS

The RSS feed can be served as any other text file, though the headers will need to be set to assist the client application. Let's take a look at the following code snippet for more information:

  void _serveRSSFeed(HttpRequest request) {
    request.response
      ..statusCode = HttpStatus.OK
      ..headers.set('Content-Type', 'application/rss+xml')
      ..write(hostedBlog.getRSSfeed())
      ..close();
  }

The content type is set to application/rss+xml, which triggers the XML display in the browser. Otherwise, it may attempt to display the content as HTML.

主站蜘蛛池模板: 巫溪县| 增城市| 伊吾县| 斗六市| 苍南县| 襄樊市| 双柏县| 吉林省| 镇赉县| 安丘市| 虞城县| 合水县| 合水县| 锦屏县| 崇左市| 华宁县| 叙永县| 东莞市| 广州市| 教育| 潞西市| 察雅县| 开化县| 阳高县| 马龙县| 嘉荫县| 琼海市| 明水县| 海南省| 卓尼县| 林口县| 新郑市| 红桥区| 修武县| 彰化县| 西乌珠穆沁旗| 凉城县| 蓝山县| 通江县| 淳化县| 阿荣旗|