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?

Community
  • 1
  • 1
James Skidmore
  • 49,340
  • 32
  • 108
  • 136
  • Have you looked at the contents of node in the debugger? When you say won't work, does that mean duration is nil or you get an exception? – falconcreek Jul 08 '10 at 22:26

1 Answers1

2

"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.

James Skidmore
  • 49,340
  • 32
  • 108
  • 136
porges
  • 30,133
  • 4
  • 83
  • 114
  • Thanks Porges, I think that is exactly right. But now there's another issue -- see my edit above. – James Skidmore Jul 09 '10 at 00:41
  • 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 Jul 09 '10 at 00:57
  • 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 Jul 09 '10 at 01:02
  • Actually, after looking at the TouchXML source, it looks like just `elementsForName: @"duration"` might work... ignoring namespaces completely. – porges Jul 09 '10 at 01:13
  • You're absolutely right, that worked. Thank you for looking into the source code, this is a big help! – James Skidmore Jul 09 '10 at 01:28