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

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.

主站蜘蛛池模板: 茂名市| 唐海县| 万载县| 辉南县| 乡宁县| 高阳县| 从化市| 三门县| 特克斯县| 平凉市| 武陟县| 花莲县| 扶沟县| 临夏市| 奉贤区| 嵩明县| 罗城| 翁源县| 吉隆县| 松江区| 大冶市| 射洪县| 车致| 团风县| 潞西市| 东港市| 宁阳县| 湖南省| 渑池县| 英超| 桦南县| 岱山县| 西平县| 阿拉善盟| 杨浦区| 疏附县| 连山| 青岛市| 元江| 镇巴县| 平邑县|