krot

exml build_stanza

Sep 25th, 2019
713
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -module(rc).
  2. -export([init/0]).
  3.  
  4.  
  5. -include_lib("include/exml.hrl").  
  6.  
  7.    
  8. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
  9. build_stanza(false)->false;
  10. build_stanza(M) when is_map(M) -> build_stanza(maps:to_list(M),[]);
  11. build_stanza(L) when is_list(L) -> build_stanza(L,[]).
  12.  
  13. %build_stanza([],[E1,E2])    ->[E1,E2];
  14. %build_stanza([],Acc) when is_list(Acc)    ->[X]=Acc,X;
  15. build_stanza([],Acc)      -> Acc;
  16. build_stanza(M,Acc) when is_map(M)  ->build_stanza(maps:to_list(M),Acc);
  17.  
  18. build_stanza([{K,Z}|H],Acc)   ->
  19.     Attrs = build_stanza_attr(maps:get(<<"attr">>,Z,false)),
  20.     Value =  maps:get(<<"value">>,Z,false),
  21.     Children =  build_stanza(maps:get(<<"children">>,Z,false)),
  22.    
  23.       R= #xmlel{name = K},
  24.      
  25.       case Attrs of
  26.         false -> R2 = R;
  27.         _ -> R2=R#xmlel{attrs = Attrs}
  28.       end,
  29.      
  30.       case Value of
  31.         false -> R3 = R2;
  32.         _ -> R3=R2#xmlel{children = [{xmlcdata, Value}]}
  33.       end,
  34.      
  35.       case Children of
  36.         false -> R4 = R3;
  37.         _ -> %Ch=R3#xmlel.children,
  38.              %case Ch of
  39.             %   [] -> R4=R3#xmlel{children = [  Children]};
  40.             %   _ -> R4=R3#xmlel{children = [ Ch|Children]}
  41.             % end
  42.             R4=R3#xmlel{children = Children}
  43.                  
  44.       end,
  45.  
  46.     build_stanza(H,[R4|Acc])
  47.    
  48. ;
  49. build_stanza(_,_)   ->false.
  50. build_stanza_attr(Attrs) when is_map(Attrs) ->maps:to_list(Attrs);
  51. build_stanza_attr(_)  -> false.
  52.  
  53. build_stanza_nr(false)    ->false;
  54. build_stanza_nr(Acc=[E1,E2])    ->Acc;
  55. build_stanza_nr(Acc) when is_list(Acc)    ->[X]=Acc,X.
  56.  
  57. build_stanza_my(Map) ->build_stanza_nr(build_stanza(Map)).
  58. %%%%%%%%%%%%%%%%%%%%%
  59. init() ->
  60.  
  61.  
  62. Map = #{
  63.                             <<"xxx">> =>#{
  64.                             <<"value">>=><<"text">>
  65.                             } ,
  66.                             <<"content">> =>#{
  67.                             <<"value">>=><<"dop content">>
  68.                             }  
  69.         },
  70.        
  71.        
  72.         Map2 = #{
  73.                             <<"xyz">> =>#{
  74.                             <<"value">>=><<"text">>
  75.                             }    
  76.         },
  77. Stanza= build_stanza_my(Map),
  78.  
  79.  
  80.            
  81.            
  82.  
  83.  
  84. %{ok, Parser} = exml:parse(<<"<x1 a=\"1\"><x2>v2</x2><x4>v4</x4></x1>">>), 
  85.  
  86.  
  87. Z = #xmlel{name = <<"body">>,
  88.            children = [
  89.                         {xmlcdata, <<"text">>}
  90.                         ]},
  91.                        
  92.                        
  93.                        
  94.  
  95.            
  96.            
  97.            
  98.     Attrs = [{<<"from">>, <<"from@j">>},
  99.              {<<"to">>, <<"to@j">>},
  100.              {<<"id">>, <<"id">>},
  101.              {<<"type">>, <<"groupchat">>}],
  102.     BodyEl =[#xmlel{name = <<"body">>,
  103.                     children = [
  104.                             {xmlcdata, <<"body">>}
  105.                     ]}],
  106. if
  107. is_list(Stanza) ->  Mm= #xmlel{name = <<"message">>,attrs = Attrs,children =BodyEl++Stanza};
  108. true -> Mm= #xmlel{name = <<"message">>,attrs = Attrs, children =BodyEl++[Stanza]}        
  109. end,       
  110.            
  111.            
  112. io:format("~p ~n", [exml:to_binary( Mm)])          
  113. %{Z,<<"+++">>,exml:to_binary( Mm)}         
  114. %exml:to_binary(Z)         
  115. %{xmlel,<<"message">>,[],   [{xmlel,<<"body">>,[],[{xmlcdata,<<"zyi">>}]}]}
  116. %{xmlel,<<"message">>,[],   [{xmlel,<<"body">>,[],[{xmlcdata,<<"zyi">>}]}]}        
  117. .
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×