Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(rc).
- -export([init/0]).
- -include_lib("include/exml.hrl").
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- build_stanza(false)->false;
- build_stanza(M) when is_map(M) -> build_stanza(maps:to_list(M),[]);
- build_stanza(L) when is_list(L) -> build_stanza(L,[]).
- %build_stanza([],[E1,E2]) ->[E1,E2];
- %build_stanza([],Acc) when is_list(Acc) ->[X]=Acc,X;
- build_stanza([],Acc) -> Acc;
- build_stanza(M,Acc) when is_map(M) ->build_stanza(maps:to_list(M),Acc);
- build_stanza([{K,Z}|H],Acc) ->
- Attrs = build_stanza_attr(maps:get(<<"attr">>,Z,false)),
- Value = maps:get(<<"value">>,Z,false),
- Children = build_stanza(maps:get(<<"children">>,Z,false)),
- R= #xmlel{name = K},
- case Attrs of
- false -> R2 = R;
- _ -> R2=R#xmlel{attrs = Attrs}
- end,
- case Value of
- false -> R3 = R2;
- _ -> R3=R2#xmlel{children = [{xmlcdata, Value}]}
- end,
- case Children of
- false -> R4 = R3;
- _ -> %Ch=R3#xmlel.children,
- %case Ch of
- % [] -> R4=R3#xmlel{children = [ Children]};
- % _ -> R4=R3#xmlel{children = [ Ch|Children]}
- % end
- R4=R3#xmlel{children = Children}
- end,
- build_stanza(H,[R4|Acc])
- ;
- build_stanza(_,_) ->false.
- build_stanza_attr(Attrs) when is_map(Attrs) ->maps:to_list(Attrs);
- build_stanza_attr(_) -> false.
- build_stanza_nr(false) ->false;
- build_stanza_nr(Acc=[E1,E2]) ->Acc;
- build_stanza_nr(Acc) when is_list(Acc) ->[X]=Acc,X.
- build_stanza_my(Map) ->build_stanza_nr(build_stanza(Map)).
- %%%%%%%%%%%%%%%%%%%%%
- init() ->
- Map = #{
- <<"xxx">> =>#{
- <<"value">>=><<"text">>
- } ,
- <<"content">> =>#{
- <<"value">>=><<"dop content">>
- }
- },
- Map2 = #{
- <<"xyz">> =>#{
- <<"value">>=><<"text">>
- }
- },
- Stanza= build_stanza_my(Map),
- %{ok, Parser} = exml:parse(<<"<x1 a=\"1\"><x2>v2</x2><x4>v4</x4></x1>">>),
- Z = #xmlel{name = <<"body">>,
- children = [
- {xmlcdata, <<"text">>}
- ]},
- Attrs = [{<<"from">>, <<"from@j">>},
- {<<"to">>, <<"to@j">>},
- {<<"id">>, <<"id">>},
- {<<"type">>, <<"groupchat">>}],
- BodyEl =[#xmlel{name = <<"body">>,
- children = [
- {xmlcdata, <<"body">>}
- ]}],
- if
- is_list(Stanza) -> Mm= #xmlel{name = <<"message">>,attrs = Attrs,children =BodyEl++Stanza};
- true -> Mm= #xmlel{name = <<"message">>,attrs = Attrs, children =BodyEl++[Stanza]}
- end,
- io:format("~p ~n", [exml:to_binary( Mm)])
- %{Z,<<"+++">>,exml:to_binary( Mm)}
- %exml:to_binary(Z)
- %{xmlel,<<"message">>,[], [{xmlel,<<"body">>,[],[{xmlcdata,<<"zyi">>}]}]}
- %{xmlel,<<"message">>,[], [{xmlel,<<"body">>,[],[{xmlcdata,<<"zyi">>}]}]}
- .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement