views:

89

answers:

1

I have the following code:

NSString *subtitle = [[[node elementsForName:@"subtitle"] objectAtIndex:0] stringValue];
NSString *duration = [[[node elementsForName:@"itunes:duration"] objectAtIndex:0] stringValue];

The first line works perfectly. The second line though won't work. I assume it has something to do with namespaces, but I'm pretty new to all of this so I would appreciate any guidance. Thank you!


It turns out that I can use the elementsForLocalName:URI: to read the element correctly. Now the problem is that since I am using the TouchXML library, it doesn't seem like that method has been mapped over to the CXMLElement structure (see here).

So the question now is: how can I convert a CXMLElement to an NSXMLElement so that I can use that method?

+1  A: 

"itunes" is the namespace identifier. It doesn't actually have any significance on its own, it just links a URI with the element in question. It looks like you're using the iTunes RSS extensions, which live under the namespace http://www.itunes.com/dtds/podcast-1.0.dtd.

So, for namespaced elements, I think (I'm not familiar with Objective-C or NSXML :P) you want to use elementsForLocalName instead:

[node elementsForLocalName: @"duration" URI: @"http://www.itunes.com/dtds/podcast-1.0.dtd"]

For the answer to the second question, see comments below.

Porges
Thanks Porges, I think that is exactly right. But now there's another issue -- see my edit above.
James Skidmore
It looks like, if you can get the actual prefix used by the XML document (so whatever `xmlns:____="http://www.itunes.com/dtds/podcast-1.0.dtd"` is), you should be able to just use `elementsForName: @"____:duration"`.
Porges
That's not working either -- the _____ is "itunes". Looks like I may need to either switch to another library or somehow make a re-formatted proxy for the XML feed from iTunes. I appreciate your help Porges!
James Skidmore
Actually, after looking at the TouchXML source, it looks like just `elementsForName: @"duration"` might work... ignoring namespaces completely.
Porges
You're absolutely right, that worked. Thank you for looking into the source code, this is a big help!
James Skidmore