[erlang-questions] Rant: I hate parsing XML with Erlang

Ulf Wiger ulf@REDACTED
Wed Oct 24 10:00:51 CEST 2007


2007/10/23, Hakan Mattsson <hakan@REDACTED>:
> On Tue, 23 Oct 2007, Joel Reymont wrote:
>
>
> At a first glance your Ruby code looks impressively
> compact.  But the corresponding implementation in
> Erlang is about the same size. What's the point in
> adding some syntactic sugar in order to make it even
> more compact? It is just a matter of taste.
>
>     % cat product.erl
>     -module(product).
>     -compile(export_all).
>     -include_lib("xmerl/include/xmerl.hrl").
>
>      parse(File) ->
>         {#xmlElement{content = Exports}, _} = xmerl_scan:file(File),
>         [{Tag, Val} || #xmlElement{content = Products} <- Exports,
>                        #xmlElement{content = Fields} <- Products,
>                        #xmlText{parents = [{Tag, _} | _], value = Val}  <- Fields].
>
>     % erl
>     Erlang (BEAM) emulator version 5.5.5 [source] [async-threads:0] [kernel-poll:false]
>
>     Eshell V5.5.5  (abort with ^G)
>     1> product:parse("my.xml").
>     [{'SKU',"403276"},{'ItemName',"Trivet"},{'CollectionNo',"0"},{'Pages',"0"}]
>     2>

There is a function, xmerl_lib:simplify_element(E), which accomplishes the
same thing:

1> {Data,_} = xmerl_scan:string(Str),xmerl_lib:simplify_element(Data).
{'Export',[],
          ["\n\n  ",
           {'Product',[],
                      ["\n\n    ",
                       {'SKU',[],["403276"]},
                       "\n\n    ",
                       {'ItemName',[],["Trivet"]},
                       "\n\n    ",
                       {'CollectionNo',[],["0"]},
                       "\n\n    ",
                       {'Pages',[],["0"]},
                       "\n\n  "]},
           "\n\n"]}

Unfortunately, it's not documented.

BR,
Ulf W



More information about the erlang-questions mailing list