Created
October 20, 2011 06:40
-
-
Save hanksudo/1300564 to your computer and use it in GitHub Desktop.
simple sample to parse XML by xmerl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%% 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