1
今天我想嘗試解析URL中的xml數據。我使用NSXMLParser來獲取xml數據,並在我的應用程序中處理它。現在,問題是當我嘗試記錄結果時,我剛剛分析了一個項目。有沒有我錯過了...?iOS - 解析XML數據無法獲取所有數據
我想要的是嘗試獲取此xml上的所有項目。但是,爲什麼我只得到一個項目解析..?任何人都可以幫助我..?
xml數據。
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<generator>Feed Generator</generator>
<title>Title</title>
<link>http://m.example.com/index.html</link>
<description>semua description tentang situs ini</description>
<language>id-ID</language>
<pubDate>Thu, 08 Dec 2011 18:00:51 +0700</pubDate>
<lastBuildDate>Thu, 08 Dec 2011 18:00:51 +0700</lastBuildDate>
<webMaster>[email protected] (Editor)</webMaster>
<image>
<title>Image Title/title>
<url>http://imagelink.com/image.jpg</url>
<link>http://imagelink.com/imageindex.html</link>
</image>
<item>
<title><![CDATA[Article Title]]></title>
<link>http://article-example.com</link>
<pubDate>Thu, 08 Dec 2011 17:23:00 +0700</pubDate>
<dc:creator>example.com</dc:creator>
<category><![CDATA[]]></category>
<guid isPermaLink="false">0000380840</guid>
<description><![CDATA[article description]]></description>
<content:encoded><![CDATA[article content]]></content:encoded>
<enclosure url="http://imageurl.com/image.jpg" length="10240" type="image/jpg"/>
</item>
<item>
<title><![CDATA[Article Title]]></title>
<link>http://article-example.com</link>
<pubDate>Thu, 08 Dec 2011 17:23:00 +0700</pubDate>
<dc:creator>example.com</dc:creator>
<category><![CDATA[]]></category>
<guid isPermaLink="false">0000380840</guid>
<description><![CDATA[article description]]></description>
<content:encoded><![CDATA[article content]]></content:encoded>
<enclosure url="http://imageurl.com/image.jpg" length="10240" type="image/jpg"/>
</item>
<item>
<title><![CDATA[Article Title]]></title>
<link>http://article-example.com</link>
<pubDate>Thu, 08 Dec 2011 17:23:00 +0700</pubDate>
<dc:creator>example.com</dc:creator>
<category><![CDATA[]]></category>
<guid isPermaLink="false">0000380840</guid>
<description><![CDATA[article description]]></description>
<content:encoded><![CDATA[article content]]></content:encoded>
<enclosure url="http://imageurl.com/image.jpg" length="10240" type="image/jpg"/>
</item>
</channel>
</rss>
這是我的代碼。
file .h
NSXMLParser *rssParser;
NSMutableArray *articles;
NSMutableDictionary *item;
NSMutableString *currentElement;
NSData *xmlFile;
NSMutableString *elementValue, *feedTitle, *feedDate, *feedDesc, *feedID, *feedEnclosure, *feedContent, *feedLink;
BOOL errorParsing;
。
file .m
/////////XML PARSE/////////////
- (void)parseXMLFileAtURL:(NSString *)URL{
NSString *agentString = @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
[request setValue:agentString forHTTPHeaderField:@"User-Agent"];
xmlFile = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
articles = [[NSMutableArray alloc] init];
errorParsing=NO;
rssParser = [[NSXMLParser alloc] initWithData:xmlFile];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:YES];
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSString *errorString = [NSString stringWithFormat:@"Error code %i", [parseError code]];
NSLog(@"Error parsing XML: %@", errorString);
NSLog(@"Check your internet connection: %@", errorString);
errorParsing=YES;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
elementValue = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
feedTitle = [[NSMutableString alloc] init];
feedDate = [[NSMutableString alloc] init];
feedContent = [[NSMutableString alloc] init];
feedLink = [[NSMutableString alloc] init];
feedDesc = [[NSMutableString alloc] init];
feedID = [[NSMutableString alloc] init];
feedEnclosure = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([elementValue isEqualToString:@"title"]) {
[feedTitle appendString:string];
}else if ([elementValue isEqualToString:@"link"]){
[feedLink appendString:string];
}else if ([elementValue isEqualToString:@"content:encoded"]){
[feedContent appendString:string];
}else if ([elementValue isEqualToString:@"pubDate"]){
[feedDate appendString:string];
}else if ([elementValue isEqualToString:@"guid"]){
[feedID appendString:string];
}else if ([elementValue isEqualToString:@"enclosure"]){
[feedEnclosure appendString:string];
}else if ([elementValue isEqualToString:@"description"]){
[feedDesc appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {
[item setObject:feedTitle forKey:@"title"];
[item setObject:feedLink forKey:@"link"];
[item setObject:feedDate forKey:@"pubDate"];
[item setObject:feedContent forKey:@"content:encoded"];
[item setObject:feedDesc forKey:@"description"];
[item setObject:feedEnclosure forKey:@"enclosure"];
[item setObject:feedID forKey:@"guid"];
[articles addObject:item];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
if (errorParsing == NO) {
NSLog(@"ID:%@", feedID);
NSLog(@"title:%@", feedTitle);
NSLog(@"date created: %@", feedDate);
NSLog(@"%@", feedEnclosure);
NSLog(@"%@", feedContent);
NSLog(@"XML processing done!");
}else{
NSLog(@"error processing xml file");
}
}
////////////////////////////////
。
結果
2011-12-08 18:51:18.305 FlipView[1537:f803] ID:0000380840
2011-12-08 18:51:18.306 FlipView[1537:f803] title:Article Title
2011-12-08 18:51:18.307 FlipView[1537:f803] date created: Mon, 08 Dec 2011 17:23:00 +0700
2011-12-08 18:51:18.307 FlipView[1537:f803]
2011-12-08 18:51:18.308 FlipView[1537:f803] article content
2011-12-08 18:51:18.308 FlipView[1537:f803] XML processing done!
那麼,有沒有解決方案給我..? – andikurnia
見編輯的評論 –
啊,它的工作....謝謝理查德... :) – andikurnia