erlang xml 处理

本文详细介绍了一种在Erlang中处理XML数据的方法,包括从字符串或文件中解析XML,将XML转换为列表,以及将列表转换回XML。通过具体函数实现,展示了如何查找特定字段,解码XML内容,并重新编码为所需格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解出xml Str字段 Str:查找字段   XmlStr:xml格式的string

string(Str, XmlStr) ->
    {Doc, _} = xmerl_scan:string(XmlStr),
    Date = xmerl_xpath:string(Str, Doc),
    if
        Date =/= [] ->
            [#xmlElement{content = Content}] = Date,
            [#xmlText{value = Val}] = Content,
            Val;
        true ->
            ""
    end.

文件解出xml Str字段   Str:查找字段  XmlFile:xml文件位置

file(Str, XmlFile) ->
    {Doc, _} = xmerl_scan:file(XmlFile),
    Date = xmerl_xpath:string(Str, Doc),
    if
        Date =/= [] ->
            [#xmlElement{content = Content}] = Date,
            [#xmlText{value = Val}] = Content,
            Val;
        true ->
            ""
    end.

文件解出xml Str字段转为列表形式  XmlFile:文件位置

decode_file(XmlFile) ->
    {Doc, _} = xmerl_scan:file(XmlFile),
    Content = decode_handler(Doc),
    lists:foldl(
        fun(#xmlText{pos = Pos, parents = Parents, value = Value}, TextL1) ->
            {Key, _} = lists:nth(Pos, Parents),
            [{Key, Value} | TextL1]
        end, [], Content).

转成xml 转为列表形式     Xml:xml格式的string

decode(Xml) ->
    ContentList = decode_shift(util:to_list(Xml), []),
    TextL =
        lists:foldl(
            fun(#xmlText{pos = Pos, parents = Parents, value = Value}, TextL1) ->
                {Key, _} = lists:nth(Pos, Parents),
                [{Key, Value} | TextL1]
            end, [], ContentList),
    TextL.

%% 转换内容的解析内容
decode_shift("", L) ->
    L;
decode_shift(Xml, L) ->
    {Doc, Doc1} = xmerl_scan:string(Xml),
    Content = decode_handler(Doc),
    decode_shift(Doc1, Content ++ L).

处理内容的解析内容

decode_handler(Element) ->
    lists:flatten(decode_handler(Element, [])).
decode_handler(Element, L) ->
    case Element of
        #xmlText{pos = 1} ->
            [Element | L];
        #xmlElement{content = Content1} ->
            decode_handler(Content1, []) ++ L;
        ElementList when is_list(ElementList) ->
            [decode_handler(Element1, []) || Element1 <- ElementList];
        _ ->
            L
    end.

列表 转成xml     格式:List=[{key,value}|...]

encode(List) ->
    ParamList = encode_handler(List, []),
    XmlList = xmerl:export_simple_content(ParamList, xmerl_xml),
    "<xml>" ++ lists:flatten(XmlList) ++ "</xml>".
encode_handler([], L) ->
    L;
encode_handler([{Key, Value} | List], L) ->
    NewValue =
        case is_list(Value) of
            true ->
                case hd(Value) of
                    {_, _} ->
                        encode_handler(Value, []);
                    _ ->
                        case is_change_list(Value) of
                            false ->
                                io_lib:format("~p", [Value]);
                            _ ->
                                [Value]
                        end
                end;
            _ ->
                [util:to_list(Value)]
        end,
    encode_handler(List, [{util:to_atom(Key), NewValue} | L]).
is_change_list([]) ->
    true;
is_change_list([S | Str]) ->
    case is_integer(S) of
        true ->
            is_change_list(Str);
        _ ->
            false
    end.

 

方法只是平时工作时处理的,如果有不同见解,请留言讨论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值