Search code examples
javaxmlxmppsmack

How to send custom XML data using the Smack library?


I'm using the Smack API in Java to connect to my XMPP server.

I want to send a customized message packet like this:

<message to="you@MyServer.com" type="chat" MYFIELD="custom stuff">
    <body> hi </body>
    <CUSTOM_STANZA A="..." B="..."> 
        C="..." 
        D="..."
    </CUSTOM_STANZA>
</message>

I'm guessing that I create implement my own Packet with that returns this XML in it's toXML() method. But that doesn't seem to work.

Any help would be appreciated.


Solution

  • You need to define a custom class that should implements ExtensionElement (as menitioned by @Flow)

    A very detailed explanation that produces the following stanza is available in this answer

    <message id='923442621149' type='chat'><body>shanraisshan</body>
    <reply xmlns='shayan:reply' rText='this is custom attribute'/>
    </message>
    

    where reply is a custom extension, which contains

    1. Element (reply)
    2. Namespace (shayan:reply)

    the list of default xmpp namespaces are available at Official XMPP website