Extending a Stanza (cross posted)

Hi all,

I’m just looking for a way to get me started…

I want to extend a stanza (probably Presence) with my own namespace and some structured information within it.

I’m assuming I just write a packetListener and do something with the packets… but I’m hoping that I don’t need to get down to editing raw XML do I?

Does anyone know (or even better have an example) of the code needed to create a new packet and extend it with my new XML?

Many thanks

(Cross posted from the Dev forum - sorry, I’ll remove whichever one gets answered as I’m not sure where to put this question)

Hi ,

You don’t need to create new packet you can extend your custom stanza with your namesapce and element.

probably you want to do like

<presence from="you@MyServer.com"> <x xmlms="http://mycompany.com/mycustomnamespace1> <custom_data/> </x> </presence>

you can do it with extending packet extension in presence packet.

PacketExtension example = new PacketExtension(“x”,“namespace”);

example.getElement().addElement(“custom_data”);

org.xmpp.packet.Presence p = new org.xmpp.packet.Presence();

p.addExtension(example);

hope it will help…

Thanks Hiren, this is almost a perfect answer. With a small tweak it works.

I havent quite worked out the syntax for adding nested elements using the packet extension is it something like :

example.getElement(“custom_element”).addElement(“new_subElement”):

Thanks again for a fantastic answer.

In case anyone comes across and needs another example, I solved this with the following snippet:

Log.debug(“Packet Interception started”);

setNameSpace(“http://chat.mydomain.com/information/DI”);

if (packet instanceof Message && ((Message) packet).getBody() != null && incoming) {

Log.debug(“Setting up packet extension”);

PacketExtension pe= new PacketExtension(“x”, getNameSpace());

DocumentFactory factory = DocumentFactory.getInstance();

Document doc = factory.createDocument();

Element root = doc.addElement(“DI”);

root.addText(“Test01”);

pe.getElement().add(root);

packet.addExtension(pe);

}