PDA

View Full Version : Question About RSS Feeds and NSXML




macprogrammer80
May 3, 2009, 04:35 AM
I am currently taking the data from a Twitter XML feed. According to the Twitter documentation, it returns the data as:

<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<id>1401881</id>
<name>Doug Williams</name>
<screen_name>dougw</screen_name>
<location>San Francisco, CA</location>
<description>Twitter API Support. Internet, greed, users, dougw and opportunities are my passions.</description>
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/59648642/avatar_normal.png</profile_image_url>
<url>http://www.igudo.com</url>
<protected>false</protected>
<followers_count>1031</followers_count>
<profile_background_color>9ae4e8</profile_background_color>
<profile_text_color>000000</profile_text_color>
<profile_link_color>0000ff</profile_link_color>
<profile_sidebar_fill_color>e0ff92</profile_sidebar_fill_color>
<profile_sidebar_border_color>87bc44</profile_sidebar_border_color>
<friends_count>293</friends_count>
<created_at>Sun Mar 18 06:42:26 +0000 2007</created_at>
<favourites_count>0</favourites_count>
<utc_offset>-18000</utc_offset>
<time_zone>Eastern Time (US & Canada)</time_zone>
<profile_background_image_url>http://s3.amazonaws.com/twitter_production/profile_background_images/2752608/twitter_bg_grass.jpg</profile_background_image_url>
<profile_background_tile>false</profile_background_tile>
<statuses_count>3390</statuses_count>
<notifications>false</notifications>
<following>false</following>
<status>
<created_at>Tue Apr 07 22:52:51 +0000 2009</created_at>
<id>1472669360</id>
<text>At least I can get your humor through tweets. RT @abdur: I don't mean this in a bad way, but genetically speaking your a cul-de-sac.</text>
<source><a href="http://www.tweetdeck.com/">TweetDeck</a></source>
<truncated>false</truncated>
<in_reply_to_status_id></in_reply_to_status_id>
<in_reply_to_user_id></in_reply_to_user_id>
<favorited>false</favorited>
<in_reply_to_screen_name></in_reply_to_screen_name>
</status>
</user>
... truncated ...
</users>


I have code that parses the data like so:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"item"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:@"screen_name"];
[item setObject:currentLink forKey:@"screen_name"];
[item setObject:currentSummary forKey:@"users"];
[item setObject:currentDate forKey:@"date"];

[stories addObject:[item copy]];
NSLog(@"adding story: %@", currentTitle);
}


}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"id"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"screen_name"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"users"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}

}



And it comes out in the console like:

2009-05-03 19:25:07.963 TestApp[35869:20b] ended element: id
2009-05-03 19:25:07.963 TestApp[35869:20b] ended element: text
2009-05-03 19:25:07.964 TestApp[35869:20b] ended element: source
2009-05-03 19:25:07.964 TestApp[35869:20b] ended element: truncated
2009-05-03 19:25:07.964 TestApp[35869:20b] ended element: in_reply_to_status_id
2009-05-03 19:25:07.965 TestApp[35869:20b] ended element: in_reply_to_user_id
2009-05-03 19:25:07.966 TestApp[35869:20b] ended element: favorited
2009-05-03 19:25:07.966 TestApp[35869:20b] ended element: in_reply_to_screen_name
2009-05-03 19:25:07.966 TestApp[35869:20b] ended element: status
2009-05-03 19:25:07.967 TestApp[35869:20b] ended element: user


My question is how do I change my code so it can grab the values from the data above? Currently it just loops through the


NSLog(@"ended element: %@", elementName);


for about 100 lines displaying the code excerpt above (20 times) and nothing else.



BlackWolf
May 3, 2009, 05:37 AM
well, I'm no expert in parsing xml, but obviously

if ([elementName isEqualToString:@"item"]) {

is always false. which is probably because there is no element named "item". there is an element named "id", one named "text" and so on. since there is no element named item your if-case never gets executed