Skip to content

Instantly share code, notes, and snippets.

@hanksudo
Created October 20, 2011 06:40
Show Gist options
  • 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 <[email protected]>
%%%
%%% @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