Skip to content

Instantly share code, notes, and snippets.

@hanksudo
Created October 20, 2011 06:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanksudo/1300564 to your computer and use it in GitHub Desktop.
Save hanksudo/1300564 to your computer and use it in GitHub Desktop.
simple sample to parse XML by xmerl
%%% xmltest.erl
%%%
%%% @author Hank Wang <drapho@gmail.com>
%%%
%%% @doc simple sample to parse XML by xmerl
%%%
-module(xmltest).
-include_lib("xmerl/include/xmerl.hrl").
-export([main/0]).
main() ->
Body = "
<Bookstore>
<Book>
<ISBN>9781401309657</ISBN>
<Name>The Last Letcture</Name>
<Author>Randy Pausch</Author>
</Book>
</Bookstore>",
{Xml, _} = xmerl_scan:string(Body),
[val(xmerl_xpath:string("//ISBN", Xml)),
val(xmerl_xpath:string("//Name", Xml)),
val(xmerl_xpath:string("//Author", Xml))
].
val(X) ->
[#xmlElement{name = N, content = [#xmlText{value = V}|_]}] = X,
{N, V}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment