使用GDataXML解析包含的XML文件

时间:2012-08-21 17:32:01

标签: iphone xml xcode parsing gdata

此XML将包含在应用程序中:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>

    <title>Directory</title>
    <description>Directory of Members</description>
    <language>en</language>
    <item>
        <LastName></LastName>
        <FirstName></FirstName>
        <Address></Address>
        <Phone></Phone>
        <Email></Email>
    </item>
    <item>
        <LastName></LastName>
        <FirstName></FirstName>
        <Address></Address>
        <Phone></Phone>
        <Email></Email>
    </item>
</channel>
</rss>

我希望能够解析XML并将其存储到我之前使用其他XML完成的RSSEntry类中。问题是我使用的唯一其他XML已经在线定位,并且我使用ASIHttpRequest类作为方法的一部分,并且不确定如何更改代码以使其适合。我目前使用此代码:

- (void)viewDidLoad {
[super viewDidLoad];   
self.allEntries = [NSMutableArray array];
self.queue = [[[NSOperationQueue alloc] init] autorelease];
self.feeds = [NSArray arrayWithObjects:@"http://316apps.com/LakesideNews/feed/",
              nil];   
NSLog(@"%@", self.feeds);

   [self refresh];    
}
- (void)refresh {

for (NSString *feed in _feeds) {
    NSURL *url = [NSURL URLWithString:feed];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [_queue addOperation:request];
}

}
- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {

NSArray *channels = [rootElement elementsForName:@"channel"];
for (GDataXMLElement *channel in channels) {            

    NSString *blogTitle = [channel valueForChild:@"title"];                    

    NSArray *items = [channel elementsForName:@"item"];
    for (GDataXMLElement *item in items) {
        NSString *articleTitle = [item valueForChild:@"title"];
        NSString *articleUrl = [item valueForChild:@"guid"];            
        NSString *articleDateString = [item valueForChild:@"pubDate"];        
        NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
        NSString *articleImage = [item valueForChild:@"content:encoded"];
        RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                                                  articleTitle:articleTitle 
                                                    articleUrl:articleUrl 
                                                   articleDate:articleDate
                                                  articleImage:articleImage] autorelease];


        [entries addObject:entry];
    }      
}

}
- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {

if ([rootElement.name compare:@"rss"] == NSOrderedSame) {
    [self parseRss:rootElement entries:entries];
} else if ([rootElement.name compare:@"feed"] == NSOrderedSame) {                       
    [self parseAtom:rootElement entries:entries];
} else {
    NSLog(@"Unsupported root element: %@", rootElement.name);
}    
}

- (void)requestFinished:(ASIHTTPRequest *)request {

[_queue addOperationWithBlock:^{

    NSError *error;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData] 
                                                           options:0 error:&error];
    if (doc == nil) { 
        NSLog(@"Failed to parse %@", request.url);
    } else {

        NSMutableArray *entries = [NSMutableArray array];
        [self parseFeed:doc.rootElement entries:entries];                

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            for (RSSEntry *entry in entries) {

                int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                    RSSEntry *entry1 = (RSSEntry *) a;
                    RSSEntry *entry2 = (RSSEntry *) b;
                    return [entry1.articleDate compare:entry2.articleDate];
                }];

                [_allEntries insertObject:entry atIndex:insertIdx];
                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                      withRowAnimation:UITableViewRowAnimationRight];

            }                            

        }];

    }        
}];

}

- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
NSLog(@"Error: %@", error);
[self refresh];
}

我知道将Title的所有区域更改为Family name等等,我只是不知道如何让GDataXML开始解析嵌套的XML。

1 个答案:

答案 0 :(得分:0)

Ray Wenderlich有一个很棒的教程,涵盖了你正在做的事情。我会从那里开始。 Here's the link