A little bit more RESTful

So my last blog post about my work on web services integration: GNOME web services integration: Introducing librest was quite some time ago. Apologies, things have been pretty busy with Moblin.

There are two traditional models for parsing XML, SAX and DOM based methods. These are both pretty heavyweight technologies but for the work i’m doing on web services I want something a bit lighter and a bit simpler to extract just the information I care about. Inspired by the Beautiful Soup HTML screen-scraping library I sought to build an API that lets you trivially find the content you are looking for.

Here is an example:

n = rest_xml_node_find (root, “photo”);
while (n)
{
  title = rest_xml_node_find (n, “title”);
  printf (“%s”, title->content);
  n = n->next;
}

Fundamentally the libREST XML parsing strategy turns the XML into a tree of nodes, where a node can have siblings (accessed through a next pointer) or childen (stored in a hash of child tag name to node). This combined with a simple tree walking function (rest_xml_node_find) results in very minimalistic code for extracting just the content you care about.

You can find the source code for libREST in git.

3 thoughts on “A little bit more RESTful

  1. ingo

    What about XPath? It’s a well known method for that sort of thing and supports Iterators, so it could fold the code above into one expression.

    Reply
  2. Benjamin Otte

    This is pretty much the same idea as jquery, isn’t it? Only that you don’t have the whole seletor engine yet 😉

    Reply
  3. robster

    @ingo: So, yeh it’s kinda like XPath, but you have you seen the libxml2 xpath implementation documentation? It’s really overkill if you want to do something really really simple; for web services based stuff this is all we’ve needed so far. 🙂

    Reply

Leave a Reply to ingo Cancel reply

Your email address will not be published. Required fields are marked *