Send cutom IQ packet


I’m trying to send a custom IQ Packet but I don’t know how to fill the XML into a IQ Packet object…

This is my code :

private void sendCustomPacket(JID from) {
        final IQ iq = new IQ();
        String output =  "<pubsub xmlns=''>";
        output += "<publish node=''>";
        output += "<item>";
        output += "<custom xml:lang='en'>";
        output += "<attribute1>1001</attribute1>";
        output += "<attribute2>2002</attribute2>";
        output += "</custom>";
        output += "</item>";
        output += "</publish>";
        output += "</pubsub>";
        iq.setChildElement(output);         iq.setType(IQ.Type.result);
        iq.setTo(from);         // Here I need to fill the 'iq' object with the xml string from 'output'... ?!         XMPPServer.getInstance().getPacketRouter().route(iq);"Log: XMPPServer.getInstance().getPacketRouter().route(iq)");

Is there any best ways to something like that ?

Here is a snippet from pubsub.

IQ result = IQ.createResultIQ(originalRequest);

    Element pubsubElem = result.setChildElement("pubsub", "[](");

Element items = pubsubElem.addElement(“items”);

items.addAttribute(“node”, getNodeID());

for (PublishedItem publishedItem : publishedItems) {

Element item = items.addElement(“item”);

if (isItemRequired()) {

item.addAttribute(“id”, publishedItem.getID());


if ((forceToIncludePayload || isPayloadDelivered()) &&

publishedItem.getPayload() != null) {




This should give you all you need for your custom packet.

I am curious about what you are trying to accomplish though, as you are sending a publish item request as a result.