It’s just data

Parsing Atom with Erlang

A simple program for parsing memes.atom.  Below is an annotated version.

-module(memes).
-export([scan/0]).
-include_lib("xmerl/include/xmerl.hrl").

Define a module named memes that exports a single function named scan which takes zero parameters.  Include the headers for xmerl, a library for processing XML.

memes_url() ->
  "http://planet.intertwingly.net/memes.atom".

Define a simple function that returns a constant string.  Some people prefer to use macros for things like this.

scan() ->
  application:start(inets),
  { ok, {_Status, _Headers, Body }} = http:request(memes_url()),
  { Xml, _Rest } = xmerl_scan:string(Body),
  format_entries(xmerl_xpath:string("//entry",Xml)),
  init:stop().

Main program

format_entries([]) -> done;
format_entries([Node|Rest]) ->
  [ #xmlText{value=Title} ] = xmerl_xpath:string("title/text()", Node),
  [ #xmlAttribute{value=Link} ] = xmerl_xpath:string("link/@href", Node),
  Message = xmerl:export_simple_content([{a,[{href,Link}],[Title]}],xmerl_xml),
  io:format('~s~n', [xmerl_ucs:to_utf8(Message)]),
  format_entries(Rest).

In lieu of looping constructs, Erlang programs tend to use sequential logic and pattern matching.

Clearly dumping XHTML fragments to stdout isn’t ideal (perhaps XHTML-IM instead?), and you wouldn’t want to dump every meme on every run, but those are problems for another day.


[from jaredhanson] Sam Ruby: Parsing Atom with Erlang

[link]...

Excerpt from del.icio.us/network/lucky_sengoku at

Sam Ruby: Parsing Atom with Erlang

simple example of something in erlang.......

Excerpt from del.icio.us/tag/erlang at

Thanks Sam! I’m just starting to learn the language, and for me XML tools are mustUnderstand (and mustHave, amongst other things [link] ). Hopefully by the time I need them, someone will have turned “XSLT like transformations in Erlang” [link] into real XSLT...

Posted by Danny at

IPC with Erlang

Previous status: I’ve shown Erlang code that sends a Jabber message.  I’ve shown Erlang code that parses a planet Venus generated Mememes feed.  Now, lets consider the problem of sending notifications for new items. Typically... [more]

Trackback from Sam Ruby

at

Nitbits

Ik maak me nu al verschillende dagen druk omwille van dit hier . Na het gedoe over Ruby en Rails (Twitter? Schaalbaarheidsproblemen? Anyone? ), en een kleine revival van PHP (hoewel, PHP5 is al drie jaar uit en blijkbaar wordt het toch niet zo druk...

Excerpt from Urgh at

links for 2007-08-30

How to Configure Swiftiply with Nginx (tags: ruby rails nginx mongrel sysadmin cluster 247up) Parsing Atom with Erlang (tags: erlang programming web xml) Full Stack: Portable Home Directory over NFS on OSX authenticated via OpenLDAP on Debian Linux...

Excerpt from Bloggitation at

Sideways Computing

On many-core, wasted cores, threads, processes, transactional memory, and the E-word....

Excerpt from Planet Atom at

Hey, Sam - just browsing about, and noticed your “http” link here ("fetch the memes_url() using...") actually points to “httpd.html”.  Thanks for all the Erlang experimentation. :)

Posted by Glen at

Fixed.  Thanks!

Posted by Sam Ruby at

This code is a bit less robust than one might like in practice.

a) http:request can fail, so you probably want a case statement to deal with errors

b) xmerl_xpath:string sometimes returns a list of multiple text nodes (for example when an entity is contained in the text, don’t ask me why), and when it does the pattern match will fail.

If you want to use this in a long-running process, both of these need to be dealt with one way or another.

Posted by Kevin Scaldeferri at

Forcing a Code Update in a Running Erlang Process

A couple days ago I wrote my first IRC bot, to echo updates from an RSS feed into an IRC channel. It seemed like a fun thing to do in Erlang, and the bot ended up being basically a splice of Jonathon Roes’ bot skeletor with Sam Ruby’s post about...

Excerpt from Kevin's Weblog at

XML parsing in Erlang

XML handling in Erlang is too hard for me. So I’ve made some survey first. Characteristics XML handling operation has two major phases. Parse XML document and make data(tree of elements) xmerl_scan (whole element tree is processed at once) SAX type...

Excerpt from kgbu//EN at

Sam Ruby: Parsing Atom with Erlang

Erlangで書かれた非常にシンプルなAtomのparser。...

Excerpt from kgbuのブックマーク / erlang / xml (5) at

Add your comment