ejabberd:如何从xml元素

时间:2016-03-29 22:00:39

标签: erlang ejabberd erl

我有这个包:

{xmlelement,"presence", [{"xml:lang","en"}, {"ver","6.00.00"}, {"to", "test@conference.localhost/user"}],
                        [{xmlelement,"c", [{"xmlns", "http://jabber.org/protocol/caps"}, {"node", "http://www.google.com/xmpp/client/caps/"}, {"ver", "eVvrsq8jya/4AZMjFl5BeDKSmg4="}, {"hash","sha-1"}], []},
                        {xmlelement,"nick", [{"xmlns", "http://jabber.org/protocol/nick"}], [{xmlcdata,<<"user">>}]},
                        {xmlelement,"x", [{"xmlns", "http://jabber.org/protocol/muc"}], [{xmlelement,"history", [{"maxstanzas","20"}, {"maxchars","32768"}], []}]}]}

我想从以下数据中提取数据:(&#34; maxchars&#34;&amp;&amp;&#34; node&#34;)

我正在做的是使用位于xml.erl中的函数,但我不知道该怎么做。

我尝试的例子:

xml:get_subtag(Packet, "maxchars")

2 个答案:

答案 0 :(得分:5)

使用最新版本的ejabberd(=不到3年),您可以按照以下步骤操作。

我假设数据包是二进制xmlel记录:

P = {xmlel,<<"presence">>, [{<<"xml:lang">>,<<"en">>}, {<<"ver">>,<<"6.00.00">>}, {<<"to">>, <<"test@conference.localhost/user">>}],
     [{xmlel,<<"c">>, [{<<"xmlns">>, <<"http://jabber.org/protocol/caps">>}, {<<"node">>, <<"http://www.google.com/xmpp/client/caps/">>}, {<<"ver">>, <<"eVvrsq8jya/4AZMjFl5BeDKSmg4=">>}, {<<"hash">>,<<"sha-1">>}], []},
      {xmlel,<<"nick">>, [{<<"xmlns">>, <<"http://jabber.org/protocol/nick">>}], [{xmlcdata,<<"user">>}]},
      {xmlel,<<"x">>, [{<<"xmlns">>, <<"http://jabber.org/protocol/muc">>}], [{xmlel,<<"history">>, [{<<"maxstanzas">>,<<"20">>}, {<<"maxchars">>,<<"32768">>}], []}]}]}.

然后你可以这样做:

X = fxml:get_subtag_with_xmlns(P, <<"x">>, <<"http://jabber.org/protocol/muc">>).                                                                                        
H = fxml:get_subtag(X, <<"history">>).
{value, MS} = fxml:get_tag_attr(<<"maxstanzas">>, H).
{value, MC} = fxml:get_tag_attr(<<"maxchars">>, H).  

MS和MC包含您的值:

MS = <<"20">>
MC = <<"32768">>

答案 1 :(得分:1)

我对此进行了测试,它非常基本,但它运行正常,虽然我很确定效率非常低和/或可能有更好的方法(等待骑兵到达):

1> X = {xmlelement,"presence", [{"xml:lang","en"}, {"ver","6.00.00"}, {"to", "test@conference.localhost/user"}], [{xmlelement,"c", [{"xmlns", "http://jabber.org/protocol/caps"}, {"node", "http://www.google.com/xmpp/client/caps/"}, {"ver", "eVvrsq8jya/4AZMjFl5BeDKSmg4="}, {"hash","sha-1"}], []}, {xmlelement,"nick", [{"xmlns", "http://jabber.org/protocol/nick"}], [{xmlcdata,<<"user">>}]}, {xmlelement,"x", [{"xmlns", "http://jabber.org/protocol/muc"}], [{xmlelement,"history", [{"maxstanzas","20"}, {"maxchars","32768"}], []}]}]}.
{xmlelement,"presence",
            [{"xml:lang","en"},
             {"ver","6.00.00"},
             {"to","test@conference.localhost/user"}],
            [{xmlelement,"c",
                         [{"xmlns","http://jabber.org/protocol/caps"},
                          {"node","http://www.google.com/xmpp/client/caps/"},
                          {"ver","eVvrsq8jya/4AZMjFl5BeDKSmg4="},
                          {"hash","sha-1"}],
                         []},
             {xmlelement,"nick",
                         [{"xmlns","http://jabber.org/protocol/nick"}],
                         [{xmlcdata,<<"user">>}]},
             {xmlelement,"x",
                         [{"xmlns","http://jabber.org/protocol/muc"}],
                         [{xmlelement,"history",
                                      [{"maxstanzas","20"},
                                       {"maxchars","32768"}],
                                      []}]}]}
2> {xmlelement,_,[{_,_},{_,_},{_,_}],[{xmlelement,_,[{_,_},{"node",Node},{_,_},{_,_}],[]},{xmlelement,_,[{_,_}],[{xmlcdata,_}]},{xmlelement,_,[{_,_}],[{xmlelement,_,[{_,_},{"maxchars",MaxChars}],[]}]}]} = X.
{xmlelement,"presence",
            [{"xml:lang","en"},
             {"ver","6.00.00"},
             {"to","test@conference.localhost/user"}],
            [{xmlelement,"c",
                         [{"xmlns","http://jabber.org/protocol/caps"},
                          {"node","http://www.google.com/xmpp/client/caps/"},
                          {"ver","eVvrsq8jya/4AZMjFl5BeDKSmg4="},
                          {"hash","sha-1"}],
                         []},
             {xmlelement,"nick",
                         [{"xmlns","http://jabber.org/protocol/nick"}],
                         [{xmlcdata,<<"user">>}]},
             {xmlelement,"x",
                         [{"xmlns","http://jabber.org/protocol/muc"}],
                         [{xmlelement,"history",
                                      [{"maxstanzas","20"},
                                       {"maxchars","32768"}],
                                      []}]}]}
3> Node.
"http://www.google.com/xmpp/client/caps/"
4> MaxChars.
"32768"
5>

在线2>,我们模式匹配您在“格式”中期望的值。将绑定放在要从中获取值的位置,如果可能,建立一些约束,例如,在XML数据包中不会更改的原子。我希望你明白这一点。