Forcing an Object into an NSString
I'm using TouchXML to parse an RSS feed.
It works pretty well until it encounters this element:
<CXMLElement 0x183a10 [0x1bdb80] item <item><title>Wenger faces greatest challenge</title><description>Manager at defining moment in Arsenal career, says Phil McNulty</description><link>http://www.bbc.co.uk/go/rss/-/blogs/philmcnulty/2011/07/arsenal.html</link><guid isPermaLink=\"false\">http://www.bbc.co.uk/blogs/philmcnulty/2011/07/arsenal.html</guid><pubDate>Mon, 04 Jul 2011 12:41:39 GMT</pubDate><category>separator</category><media:thumbnail width=\"66\" height=\"49\" url=\"http://news.bbcimg.co.uk/media/images/53844000/jpg/_53844595_wenger6649getty.jpg\"/></item>>"
All the tags get parsed out ok using the following code:
[blogItem setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
Except for the media:thumbnail which this code returns as an empty string @""
[resultElement childAtIndex:counter] stringValue]
So there stringValue part of this code fails to return anything.
If I do an NSLog of
NSLog(@"ResultElement: %@", [resultElement childAtIndex:counter] );
开发者_开发技巧It actually prints out :
ResultElement: <CXMLElement 0x4e1c310 [0x4b22eb0] media:thumbnail <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/53844000/jpg/_53844595_wenger6649getty.jpg"/>>
At this stage I know if I can get this object into an NSString or something I can use that to parse out the URL which I need.
Something like NSString *aStr = [NSString stringWithObject:[resultElement childAtIndex:counter]];
Or some special trick maybe?
Have any of you the knowledge to share as to how to make this happen?
Kindest Regards, -Code
Use :
NSString *s = [NSString stringWithFormat:@"%@", anObject];
Try finding out what the class of the object is
NSLog(@"%@",[object class]);
Once you know the class, then you can find out how to get the data from it. It looks like it might be an NSDictionary (due to the key-value pairs in your NSLog example). Therefore, you might try
[[resultElement childAtIndex:counter] objectForKey:@"url"]
to get the url from the object. The url points to an NSString (or NSURL) which you can then print out.
Just call:
NSString *stringVersion = [[resultElement childAtIndex:counter] description];
description
is part of the NSObject protocol and it's what NSLog uses to print to the console and so is guaranteed to match what you get from NSLog exactly.
However note that this is a potentially fragile approach as you'll never know if the ResultElement
class might change the way it implements description
in a future version of TouchXML, thus causing your parsing to fail.
精彩评论