|
Sparse XML Parsing An example that shows how to parse similar XML files into the same canonical HopObject structure. Here is an example that shows how to parse RSS 0.91, RSS 0.92 and RSS 1.0 files into the same canonical HopObject structure with just one line of code. You should be able to reproduce the example by following these simple steps using either today's new pre3 release or todays snapshot. # only convert XML elements for which explicit # instructions exist, and drop everything else _mode = sparse # map channel element to channel prototype rss._prototype = channel rdf\:RDF._prototype = channel channel._prototype = channel # map content of channel child elements to channel properties channel.title._text = channel.title channel.link._text = channel.link channel.description._text = channel.description # map item elements to child objects of channel rdf\:RDF.item = channel._children channel.item = channel._children # define prototype for item elements item._prototype = item # map content of item child elements to # item object properties item.link._text = link item.title._text = title item.description._text = description3) Add the following lines to a file called rss.hac in the app's root prototype: res.write ("<form>RSS URL: <input type='text' name='url' value='");
res.write (req.data.url);
res.writeln ("' size='60'>");
var url = req.data.url;
if (url) {
var obj = Xml.get(url, "apps/xmldemo/rss.properties");
var xml = Xml.writeToString (obj);
res.format("<pre><code>"+xml+"</code></pre>");
}
4) Dial up the app's rss action in your browser and enter the url of some RSS feed. When you hit enter you should see an XML dump of that RSS feed converted to a HopObject structure consisting of one channel object containing several item child objects. Note that this structure should look the same, regardless if the RSS feed was RSS 0.91, 0.92 or 1.
... comment
|
navigation
Download
Community
Weblog
Mailing Lists
IRC Channel
Documentation
Introductions
Tools
Reference
Project
Roadmap
Bug Reporting
Source
Wiki
Tags
Updates
Related Projects
search
|
||||