Cannot Read Custom Attributes From Message TAG when using asmack XMPP library for android?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 14:01:53

问题


This is the XML sent From XMPP Server Side that i want to recieve so that i can send it to my XML parser.

<message to="39@domainname.com/smack" chat_id="73392" 
       custom_packet="true" user_id="44" manager_id="39" time_stamp="0" website_id="0" 
       visitor_name="John" end_time="False" xml:lang="en-us" type="groupchat" 
       from="room73392@conference.domainname.com/39">
       <body>Hello</body> 
       <x xmlns="http://jabber.org/protocol/muc#user"> 
       <status xmlns="" code="0"/>   
       </x></message>

This Is a sample XML that i am Recieving. When i use p.toXML(); //Packet p

 <message to="44@domainname.com/Smack" 
    from="room73407@conference.domainname.com/Visitor1171" type="groupchat">
    <body>Hello</body>
    <delay xmlns="urn:xmpp:delay"></delay>
    <x xmlns="jabber:x:delay" stamp="20120917T05:57:19" 
    from="4732abb5@domainname.com/4732abb5">
    </x></message>

I have Just started using XMPP server. So any Guidance Will be Appreciated.


回答1:


You can't do this in Smack (and therefore aSmack) without modifying the source code. It will only parse a standard Message stanza, so all your custom attributes will be ignored. The proper approach in XMPP is to create extensions to the standard packets, not modify them. If you have control over what is being sent from the server, then you should change your approach to adding a custom extension to the message, thus changing this

<message to="39@domainname.com/smack" chat_id="73392" 
 custom_packet="true" user_id="44" manager_id="39" time_stamp="0" 
 website_id="0" visitor_name="John" end_time="False" xml:lang="en-us" 
 type="groupchat" from="room73392@conference.domainname.com/39">
   <body>Hello</body>
   <x xmlns="http://jabber.org/protocol/muc#user">
      <status xmlns="" code="0"/>
   </x>
</message>

to this

<message to="39@domainname.com/smack" chat_id="73392" xml:lang="en-us" 
 type="groupchat" from="room73392@conference.domainname.com/39">
   <body>Hello</body>
   <x xmlns="http://jabber.org/protocol/muc#user">
      <status xmlns="" code="0"/>
   </x>
   <custom xmlns="my:namespace:custom" user_id="44" manager_id="39" time_stamp="0" 
 website_id="0" visitor_name="John" end_time="False"/>
</message>

Then you can easily write your own provider to parse the custom packet extension and simply retrieve your custom object (created by your parser) by calling

MyExtension customStuff = message.getExtension("my:namespace:custom");

You can check out the EmbeddedExtensionProvider to very easily write your provider.




回答2:


do {
    ParseEvent event=parser.read();
    ParseEvent pe;

    switch(event.getType()){
        case Xml.START_TAG:
        if (event.getName().toString().equals("message")){
                int xx=event.getAttributeCount();

                String _s2=event.getAttribute("to").getValue();
                if(_s2=="" || _s2==null){
                    _s2="N/A";
                }

                String _s3=event.getAttribute("from").getValue();       
                if(_s3=="" || _s3==null){
                    _s3="N/A";
                }

                String _s4=event.getAttribute("type").getValue();
                if(_s4=="" || _s4==null){
                    _s4="N/A";
                }

                String _s1=_s2+"~~"+_s3+"~~"+_s4;
                m_result.add(new BeanClassName(_s1));                   
        }
        (...)
    }
}

You can read your attribues by adding the start tag for every case and then setting the value in bean Class.



来源:https://stackoverflow.com/questions/12454025/cannot-read-custom-attributes-from-message-tag-when-using-asmack-xmpp-library-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!