Hi everyone! I'm very excited to be starting my erlang journey, but I've been stuck on this for a few days now and I'm starting to get scared I won't meet my deadline.
I'm trying to create key, value pair tuples out of xml. I'd like to make a list out of any nested xml. It seems like a very common thing to do, but I can't find any examples.
For instance:
<something>
<Item>
<name>The Name!</name>
<reviews>
<review>
<review-by>WE</review-by>
<review-points>92</review-points>
</review>
<review>
<review-by>WS</review-by>
<review-points>90</review-points>
</review>
</reviews>
</Item>
</something>
Should turn out like:
[[{"name", "The Name!"}, {"reviews", [{"review-by", "WE"}, {"review-points", 92}], {"review-by", "WS"}, {"review-points", 90}]} ]]
Where each Item is the main wrapper node.
I've admittedly cargo culted and tweaked the code below. It only returns a list of the first Item's elements. And I'm not sure how to begin the nested ones.
Thanks so much!
-module(reader).
-compile(export_all).
-include_lib("xmerl/include/xmerl.hrl").
parse(FileName) ->
{Records,_} = xmerl_scan:file(FileName),
extract(Records, []).
extract(Record, Acc) when is_record(Record, xmlElement) ->
case Record#xmlElement.name of
'Item' ->
ItemData = lists:foldl(fun extract/2, [], Record#xmlElement.content),
[ {item, ItemData} | Acc ];
_ ->
lists:foldl(fun extract/2, Acc, Record#xmlElement.content)
end;
extract({xmlText, [{Attribute, _}, {'Item', 2}, _], _, _, Value, text}, Acc) ->
[{Attribute, Value}|Acc];
extract(_, Acc) ->
Acc.