使用Smack库解析XMPP的EventElement

时间:2017-09-17 08:56:52

标签: java xml xmpp smack

任何人都可以帮助向我展示如何解析此Event pub元素并获取以下Packet的Message对象。也许我的关键字/搜索术语谷歌搜索是不正确的,但我在寻找有关此文档或教程时找不到任何有用的信息。

我已经阅读了一些关于PacketParserUtils和XmlPullParser的内容并试图实现它,我一直在变为null。

代码

EventElement event = groupMessage.getExtension("event", "http://jabber.org/protocol/pubsub#event");

try {
    XmlPullParser parser = PacketParserUtils.newXmppParser();

    Log.d(TAG, "processStanza event: "+ event.toXML().toString());
    parser.setInput(new StringReader(event.toXML().toString()));
    Item  items = (Item) parser.getProperty("items");

    Log.d(TAG, "processStanza: " + items);
} catch (XmlPullParserException e) {
    e.printStackTrace();
}


<message to='+60174443333@crystal.domain.com/Resource' from='9176d3d3-e893-4add-91a1-82b42338c223@group.crystal.domain.com'>
    <event xmlns='http://jabber.org/protocol/pubsub#event'>
        <items node='urn:xmpp:mucsub:nodes:messages'>
            <item id='17100773132085304799'>
                <message xmlns='jabber:client' lang='en' to='+60174443333@crystal.domain.com' from='9176d3d3-e893-4add-91a1-82b42338c223@group.crystal.domain.com/+60149711089@crystal.domain.com' type='groupchat' id='21D85845-8434-4E0B-BB0C-5768256C5B66'>
                    <body xmlns='jabber:client'>This is the message</body>
                </message>
            </item>
        </items>
    </event>
</message>

1 个答案:

答案 0 :(得分:0)

尽管很晚,但可能会帮助新用户。 您可以将自定义muc-sub xml解析为:

(在科特林)

xmppTcpConnection.addAsyncStanzaListener(StanzaListener {
it?.let { stanza ->
  if (stanza is Message) {

    val eventElement: EventElement? = stanza.getExtension(

              "event",
              "http://jabber.org/protocol/pubsub#event") 

    if (eventElement != null) {

        val itemsExtension: ItemsExtension = eventElement .event as ItemsExtension 

        itemsExtension.items?.forEach { namedElement ->
             val payloadItem = namedElement as PayloadItem<*>

             val simplePayload: SimplePayload = payloadItem.payload as SimplePayload

             val parserString: String = simplePayload.toXML(null)

             val xmlPullParser: XmlPullParser = PacketParserUtils.getParserFor(parserString)

             val message: Message = PacketParserUtils.parseMessage(xmlPullParser)

             //Your code here
         }
       }
   }
 }
}, StanzaTypeFilter(Message::class.java))

Refer Smack Discussion here