Question

I've looked at the following XMPPFramework - Implement Group Chat (MUC) and have been successfully getting the list of public chatrooms. The list is returned from the function - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq, and I am trying to parse the information to create a plist so my iOS client can display all the chat rooms available to join.

Here is the iq being returned:

<iq xmlns="jabber:client" type="result" from="conference.example.com" to="admin@example.com/2abb8426"><query xmlns="http://jabber.org/protocol/disco#items"><item jid="test@conference.example.com" name="Test Room"/></query></iq>

My problem is that I don't know how to correctly parse the information being returned, can anyone guide me into xml parsing in this example? Thank you for your time.

Was it helpful?

Solution

Lets take as an example the official XEP page (http://xmpp.org/extensions/xep-0045.html) with the response:

<iq from='chat.shakespeare.lit'
    id='zb8q41f4'
    to='hag66@shakespeare.lit/pda'
    type='result'>
  <query xmlns='http://jabber.org/protocol/disco#items'>
    <item jid='heath@chat.shakespeare.lit'
          name='A Lonely Heath'/>
    <item jid='coven@chat.shakespeare.lit'
          name='A Dark Cave'/>
    <item jid='forres@chat.shakespeare.lit'
          name='The Palace'/>
    <item jid='inverness@chat.shakespeare.lit'
          name='Macbeth&apos;s Castle'/>
  </query>
</iq>

You can obtain the query element by doing:

NSXMLElement *queryElement = [iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"];

Then you can iterate over the items:

    NSArray *items = [queryElement elementsForName:@"item"];
    for (NSXMLElement *i in items) {
        NSString *roomName = [i attributeStringValueForName:@"name"];
        NSString *jidString = [i attributeStringValueForName:@"jid"];
        XMPPJID *jid = [XMPPJID jidWithString:jidString];
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top