sample code for consuming xmlns'd markup in custom IQs?

10 views
Skip to first unread message

Dan Brickley

unread,
Mar 17, 2010, 11:11:29 AM3/17/10
to xmppfr...@googlegroups.com

Another day, another XML question. I am now happily sending IQ messages to/from XMPPFramework. The issue now is consuming incoming markup (whether from a reply or a communication initiated elsewhere).

I have been trying to find some best practice (well, any practice!) examples for consuming XMPP XML stanzas where namespaces are used.

The following example is from a TV / media centre (MythTV if anyone's interested), sending some HTML to the handset.

<iq to="alice....@gmail.com/C795906E" type="result" from="zetland...@googlemail.com/Basicbot9E79C6DE">
<nowp-result xmlns="http://buttons.foaf.tv/">
<div><h2>Now playing</h2><p>Animal Park</p><p>On channel: bbctwo</p><p>Programme started at: 2010-03-17T14:00:00</p><p>Local channel number is: 1002</p>Description: Animal Park is a television documentary about the life of keepers and animals at Longleat Safari Park, Wiltshire, England. It also covers the daily life of workers in Longleat House, the estate and the gardens.<br/>Category: <a href="http://dbpedia.org/resource/Category:BBC_television_documentaries">http://dbpedia.org/resource/Category:BBC_television_documentaries</a><br/>Category: <a href="http://dbpedia.org/resource/Category:2000_in_British_television">http://dbpedia.org/resource/Category:2000_in_British_television</a><br/>Category: <a href="http://dbpedia.org/resource/Category:2000s_British_television_series">http://dbpedia.org/resource/Category:2000s_British_television_series</a><br/>Category: <a href="http://dbpedia.org/resource/Category:2000_television_series_debuts">http://dbpedia.org/resource/Category:2000_television_series_debuts</a><br/>Actor: <a href="http://www.imdb.com/name/nm0381702/">Kate Humble</a>
<br/>Actor: <a href="http://www.imdb.com/name/nm0283938/">Ben Fogle</a><br/>
</div>
</nowp-result>

This arrives happily in - (void)xmppClient:(XMPPClient *)sender didReceiveIQ:(XMPPIQ *)iq

... and since XMPPIQ is a kissxml node, I should be able to test to see what kind of IQ this is, and scoop out the HTML contents of <nowp-result>.

It seems this is problematic due to the use of an xmlns declaration; however my understanding is that the XMPP community prefer things done that way.

The only way I can get anything working in following code is matching the xpath ".". Anything more ambitious fails, seemingly because of the xmlns.

    NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
    [errorDetail setValue:@"Failed to decode NOWP IQ" forKey:NSLocalizedDescriptionKey];
    NSError    *error = [NSError errorWithDomain:@"buttons" code:100 userInfo:errorDetail];
//    NSArray *nowpNodes = [iq nodesForXPath:@"./iq/nowp-result" error:&error];
//ok    NSArray *nowpNodes = [iq nodesForXPath:@"." error:&error];
    NSArray *nowpNodes = [iq nodesForXPath:@"./iq/*" error:&error];

    NSLog(@"nowp results: %@", nowpNodes);
    NSEnumerator *enumerator = [nowpNodes objectEnumerator];
    id obj;    
    while ( obj = [enumerator nextObject] ) {
        printf( "%s\n", [[obj description] cString] );
    }


I searched around KissXML and NSXML without success on this. Both http://www.cocoabuilder.com/archive/cocoa/176868-nodesforxpath-almost-always-returns-no-objects.html and http://www.cocoabuilder.com/archive/cocoa/279816-ignoring-invalid-namespace-link-in-nsxmldocument.html btw suggest the Apple libraries do not behave well on this topic, so I'm not sure what ideal behaviour KissXML ought to be implementing.

Thanks for any pointers on the correct technique,

cheers,

Dan


ps. I found one of the reasons my IQ *sending* activities weren't successful before: I naively assumed Google Talk would assign me a JID of exa...@gmail.com/foo if I asked for /foo ... however it doesn't, instead you get /foo6235256 or somesuch.
Reply all
Reply to author
Forward
0 new messages