2
votes

Can anyone please help to show me on how to parse this Event pub element and get the Message object for the following Packet. Maybe my keyword/search term googling is not correct but I'm not able to find anything helpful while looking for the documentation or tutorial about this.

I have read something about PacketParserUtils and XmlPullParser and tried to implement it, I keep on getting null.

Code

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='[email protected]/Resource' from='[email protected]'>
    <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='[email protected]' from='[email protected]/[email protected]' type='groupchat' id='21D85845-8434-4E0B-BB0C-5768256C5B66'>
                    <body xmlns='jabber:client'>This is the message</body>
                </message>
            </item>
        </items>
    </event>
</message>
1
did you find the answer?gaurang

1 Answers

0
votes

Though its late, it may help new users. You can parse custom muc-sub xml as :

(In Kotlin)

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