0

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.

Sheraz Ahmad Khilji
  • 8,300
  • 9
  • 52
  • 84
  • Never invent your own attributes for XMPP top level elements. See also [XEP-134 2.1](http://xmpp.org/extensions/xep-0134.html#xmpp). Instead add an extension `/` – Flow Jan 12 '15 at 14:21

2 Answers2

4

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.

Sheraz Ahmad Khilji
  • 8,300
  • 9
  • 52
  • 84
Robin
  • 24,062
  • 5
  • 49
  • 58
  • Robin is spot on here. You're asking the wrong question. Please be careful about the namespace URI you use, however; "my:" is not a registered URI prefix that you control. If you're not going to do standards, an http:// URL that you control can be used as a URI that is guaranteed to be unique. – Joe Hildebrand Sep 18 '12 at 06:16
  • Thankyou for the Reply. So i have to modify the server side code for this. There is no way i can extract attributes from Message Stanza. – Sheraz Ahmad Khilji Sep 18 '12 at 08:26
  • I have solved this problem by modifying the source code. For More Details on how to do this Here is the Link. http://stackoverflow.com/questions/12475122/adding-custom-attributes-in-message-tag-in-xmpp-packet-using-asmack-for-android – Sheraz Ahmad Khilji Sep 19 '12 at 04:09
-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.

Emil Sierżęga
  • 1,785
  • 2
  • 31
  • 38
raghav chopra
  • 827
  • 3
  • 9
  • 25