问题描述
我非常高兴能够开始我的erlang旅程,但是我已经停留了几天了,我开始害怕我不会满足我的截止日期。我正在尝试从xml创建关键的值对元组。我想从任何嵌套的xml中列出一个列表。这似乎是一件非常常见的事情,但我找不到任何例子。
例如:
< something>
< Item>
< name> The Name!< / name>
<评论>
< 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>
应该如下:
[[{name,The Name!},{review,[{review-by,WE},{review-points,92} ,{review-by,WS},{review-points,90}]]]]
其中每个项目是主要的包装节点。
我承认货物被装载并调整了下面的代码。它只返回第一个Item的元素的列表。我不知道如何开始嵌套的。
非常感谢!
-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
'Item' - >
ItemData = lists:foldl(fun extract / 2,[],Record#xmlElement.content),
[{item,ItemData} | Acc];
_ - >
列表:foldl(fun extract / 2,Acc,Record#xmlElement.content)
end;
extract({xmlText,[{Attribute,_},{'Item',2},_],_,_,Value,text},Acc) - >
[{Attribute,Value} | Acc];
提取(_,Acc) - >
加
给尝试。函数erlsom:simple_form(XML)将给你{Node,Attrib,Value}:
{something [],
[{Item,[],
[{name,[],[The Name!]},
{评论
[{review,[],
[{review,[],[WE]},{review-points,[],[92] }]},
{review,[],
[{review-by,[],[WS]},{review-points,[], 90]}]}]}]}}}
但是,如果要删除属性那么你可以写一个乐趣传递给erlsom SAX解析器或重新格式化simple_form的输出。这样的东西(重新格式化输出):
-module(reader)。
-compile(export_all)。
convert(XML) - >
case erlsom:
{ok,{Node,_Attrib,Value},_}的simple_form(XML)
{Node,xml_to_kv(Value)};
Err->
Err
结束。
xml_to_kv([{Node,_Attrib,Value} | T]) - >
[{Node,lists:flatten(xml_to_kv(Value))} | xml_to_kv(T)];
xml_to_kv(Value) - >
价值。
这将产生以下输出:
{something,
[{Item,
[{name,The Name!},
{评论 ,
[{review,[{review-by,WE},{review-points,92}]},
{review,[{评论,WS},{review-points,90}]}]}]}}}
注意,我没有在非常大的XML文件上测试这个。您应该运行几个测试并考虑mem问题。
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.
Give Erlsom a try. The Function "erlsom:simple_form(XML)" will give you {Node,Attrib,Value}:
{"something",[],
[{"Item",[],
[{"name",[],["The Name!"]},
{"reviews",[],
[{"review",[],
[{"review-by",[],["WE"]},{"review-points",[],["92"]}]},
{"review",[],
[{"review-by",[],["WS"]},{"review-points",[],["90"]}]}]}]}]}
However, if you want to drop the Attributes then you can write a fun to pass to the erlsom SAX parser or reformat the output of simple_form. Something like this (to reformat output):
-module(reader).
-compile(export_all).
convert(XML)->
case erlsom:simple_form(XML) of
{ok,{Node,_Attrib,Value},_}->
{Node,xml_to_kv(Value)};
Err->
Err
end.
xml_to_kv([{Node,_Attrib,Value}|T])->
[{Node,lists:flatten(xml_to_kv(Value))}|xml_to_kv(T)];
xml_to_kv(Value)->
Value.
This would produce the following output:
{"something",
[{"Item",
[{"name","The Name!"},
{"reviews",
[{"review",[{"review-by","WE"},{"review-points","92"}]},
{"review",[{"review-by","WS"},{"review-points","90"}]}]}]}]}
Note, I haven't tested this on very large XML files. You should run a few tests and consider mem issues.
这篇关于Erlang xml到元组和列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!