- Dart:Scalable Application Development
- Davy Mitchell Sergey Akopkokhyants Ivo Balbaert
- 523字
- 2021-07-09 18:56:24
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.
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> ...
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.
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- iOS應用逆向工程(第2版)
- 人人都懂設計模式:從生活中領悟設計模式(Python實現(xiàn))
- TypeScript項目開發(fā)實戰(zhàn)
- Mastering Web Application Development with AngularJS
- 監(jiān)控的藝術:云原生時代的監(jiān)控框架
- Android Studio開發(fā)實戰(zhàn):從零基礎到App上線 (移動開發(fā)叢書)
- Drupal 8 Development:Beginner's Guide(Second Edition)
- Oracle Database XE 11gR2 Jump Start Guide
- 快樂編程:青少年思維訓練
- PHP+MySQL Web應用開發(fā)教程
- Learning Redux
- 菜鳥成長之路
- SQL Server 2014數據庫設計與開發(fā)教程(微課版)
- Python Business Intelligence Cookbook