【发布时间】:2013-03-22 17:24:10
【问题描述】:
我是 ios 开发的新手,我正在尝试解析 RSS 文件(xml)。
这里是 xml:(对不起语言)
<item>
<category> General < / category >
<title> killed in a tractor accident , was critically injured windsurfer </ title>
<description>
< ! [ CDATA [
<div> <a href='http://www.ynet.co.il/articles/0,7340,L-4360016,00.html'> <img src = 'http://www.ynet.co. il/PicServer3/2012/11/28/4302844/YOO_8879_a.jpg ' alt =' photo: Yaron Brener 'title =' Amona 'border = '0' width = '116 'height = '116'> </ a> < / div >
] ] >
Tractor driver in his 50s near Kfar Yuval flipped and trapped underneath . Room was critically injured windsurfer hurled rocks because of strong winds and wind surfer after was moderately injured in Netanya
< / description >
<link>
http://www.ynet.co.il/articles/0 , 7340, L- 4360016 , 00.html
< / link >
<pubDate> Fri, 22 Mar 2013 17:10:15 +0200 </ pubDate>
<guid>
http://www.ynet.co.il/articles/0 , 7340, L- 4360016 , 00.html
< / guid >
<tags> Kill , car accidents , surfing < / tags >
< / item >
这是我的 xmlparser 代码:
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
self.titles = [[NSMutableArray alloc]init];
self.descriptions = [[NSMutableArray alloc]init];
self.links = [[NSMutableArray alloc]init];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"item"]) {
isItem = YES;
}
if ([elementName isEqualToString:@"title"]) {
isTitle=YES;
self.titlesString = [[NSMutableString alloc]init];
}
if ([elementName isEqualToString:@"description"]) {
isDesription = YES;
self.descriptionString = [NSMutableString string];
self.data = [NSMutableData data];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(isItem && isTitle){
[self.titlesString appendString:string];
}
if (isItem && isDesription) {
if (self.descriptionString)
[self.descriptionString appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
if (self.data)
[self.data appendData:CDATABlock];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"item"]) {
isItem = NO;
[self.titles addObject:self.titlesString];
[self.descriptions addObject:self.descriptionString];
}
if ([elementName isEqualToString:@"title"]) {
isTitle=NO;
}
if ([elementName isEqualToString:@"description"]) {
NSString *result = [self.descriptionString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"string=%@", result);
if ([self.data length] > 0)
{
NSString *htmlSnippet = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
NSString *imageSrc = [self firstImgUrlString:htmlSnippet];
NSLog(@"img src=%@", imageSrc);
[self.links addObject:imageSrc];
}
self.descriptionString = nil;
self.data = nil;
}
}
- (NSString *)firstImgUrlString:(NSString *)string
{
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>)+?"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *result = [regex firstMatchInString:string
options:0
range:NSMakeRange(0, [string length])];
if (result)
return [string substringWithRange:[result rangeAtIndex:2]];
return nil;
}
@end
就像我说的我对 iPhone 开发很陌生,我花了几个小时寻找解决它的方法,但一无所获。 我决定开个话题,然后几个问题:
一个。解析器不会忽略 CDATA 正在解析的所有内容。 为什么会这样?正如您所看到的,描述本身不在 cdata 中,我只有第一步,但即使我没有使用 foundCDATA,我也会得到其余的:(NSData *) CDATABlock
二。我要拍图片链接,怎么办?我在网上搜索,发现很多指南解释只使用函数foundCDATA:(NSData *)CDATABlock 但它是如何使用的?我在代码中使用的方式?
我需要一个解释,以便我能理解,谢谢!
【问题讨论】:
-
如何获得该图像源的正则表达式模式
标签: iphone ios xcode xcode4.5 nsxmlparser