// We fetch the activities of the target user
List<ActivityEntry> activities = ActivityManager.getInstance().getActivities(sender.toBareJID(), target.toBareJID());
// Prepare the result packet
ActivityDomWriter writer = new DefaultActivityDomWriter();
DOMDocument domDocument = new DOMDocument();
IQ result = IQ.createResultIQ(packet);
org.dom4j.Element pubsubElement = result.setChildElement("pubsub", "http://jabber.org/protocol/pubsub");
org.dom4j.Element itemsElement = pubsubElement.addElement("items");
itemsElement.addAttribute("node", PEPActivityHandler.NODE);
for (ActivityEntry entry : activities) {
Element entryElement = (Element) domDocument.appendChild(domDocument.createElementNS(Atom.NAMESPACE, Atom.ENTRY_ELEMENT));
writer.write(entry, entryElement);
domDocument.removeChild(entryElement);
org.dom4j.Element itemElement = itemsElement.addElement("item");
itemElement.addAttribute("id", entry.getId());
itemElement.add((org.dom4j.Element) entryElement);
}