我正在尝试使用NSXMLParser解析此页面(http://www.tribunacampeche.com),但是当我放入此URL时(http://www.tribunacampeche.com/index.php?format=feed&type= rss)在parse方法中它什么都不返回。如果我尝试使用像“http://www.hello.com/hello.xml”这样的其他网址,那就可以了。
我做错了,解析此页面的正确URL是什么?
由于
这是我使用的代码:
@implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Portada";
NSURL *aURL = [[NSURL alloc] initWithString:@"http://www.tribunacampeche.com/index.php?format=feed&type=rss"];
NSError *error = nil;
NSStringEncoding encoding;
NSString * path = [[NSString alloc] initWithContentsOfURL:aURL usedEncoding:&encoding error:&error];
/*
NSString *path = @"http://www.tribunacampeche.com/index.php?format=feed&type=rss";
*/
[self parseXMLFileAtURL:path];
NSLog(@"%@",path);
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)parseXMLFileAtURL:(NSString *)URL {
// inizializziamo la lista degli elementi
elencoFeed = [[NSMutableArray alloc] init];
// dobbiamo convertire la stringa "URL" in un elemento "NSURL"
NSURL *xmlURL = [NSURL URLWithString:URL];
// inizializziamo il nostro parser XML
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
// settiamo alcune proprietà
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
// avviamo il parsing del feed RSS
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// inizializza tutti gli elementi
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {
/* salva tutte le proprietà del feed letto nell'elemento "item", per
poi inserirlo nell'array "elencoFeed" */
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"date"];
[elencoFeed addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{;
// salva i caratteri per l'elemento corrente
if ([currentElement isEqualToString:@"title"]){
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}
}
- (void) parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
这是控制台返回的内容:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Tribuna Campeche</title>
<meta http-equiv="Content-Type" content="application/vnd.wap.xhtml+xml; charset=utf-8"/>
<style type="text/css">
body{font-size:medium; vertical-align:middle; }
.topimage {TEXT-ALIGN: center;}
img{border:0px;vertical-align:middle;}
.nowrap{white-space:nowrap;}
.clr-grey {COLOR: #3F424B;}
.vsp-t {margin-top: 1ex; padding-top: 0px;}
.vsp-b {margin-bottom: 1ex; padding-bottom: 0px;}
.ft-s {FONT-SIZE: small;}
.ft-c {FONT-SIZE:7px;}
dl {margin-top: 1ex; margin-bottom: 0; margin-left: 1pt;}
</style>
<style type="text/css">
.number { -wap-input-format: "NNNNNNNNNN"; }
.zipnumber { -wap-input-format: "NNNNN"; }
.postcode { -wap-input-format: "NNNNNNN"; }
input{-wap-input-format: "*m"; format: "*m"; }
</style>