0

此 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];
}

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

4

1 回答 1

0

Ray Wenderlich 有一个很棒的教程,涵盖了你在做什么。我会从那里开始。这是链接

于 2012-08-21T17:50:13.663 回答