Helma logo
helma.org » Home > docs > examples > Sparse XML Parsing

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.

1) In a new application, create two prototypes called "channel" and "item" (by creating directories with these names in the application directory).

2) Add the following to a file called "rss.properties" in the application directory:

# 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 = description
3) 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.


Up: Code Snippets
Previous: Random Password Generator Next: Creating deep URL structure using getChildElement() and transient HopObjects

... comment


Page last modified on 2003-05-13 14:05 by hns