在研究Openfire源代码时,
查看了Smack的一些底层源代码。
发现message的property也是一个extension,
具体封装如下:
// 封装property数据 PacketExtension packetExtension = new PacketExtension("properties","http://www.jivesoftware.com/xmlns/xmpp/properties"); Element root = packetExtension.getElement(); Element propertyElement = root.addElement("property"); Element nameElement = propertyElement.addElement("name"); Element valueElement = propertyElement.addElement("value"); nameElement.setText(key); valueElement.addAttribute("type", "string"); valueElement.setText(value); sendMessage.addExtension(packetExtension); return sendMessage;
在openfire端解析Extension可以如下:
PacketExtension extension = msg.getExtension("properties", "http://www.jivesoftware.com/xmlns/xmpp/properties");
Element properties = extension.getElement();
Element property = properties.element("property");
Element value = property.element("value");
String value = value.getText();