im은 ios 개발에서 매우 새롭고 RSS 파일 (xml)을 구문 분석하려고합니다. 내가 아이폰에 아주 새로운 해요 말했듯이NSXMLParser는 CDATA를 무시하지 않습니다
- (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
:
<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가 코드 (언어에 대한 죄송) : 여기
은 XML이다 개발, 나는 그것을 몇 시간 동안 풀 수있는 방법을 찾았지만 아무 것도 발견하지 못했습니다. 주제를 연 다음 몇 가지 질문을하기로했습니다.
하나. 파서는 CDATA가 모든 것을 파싱하는 것을 무시하지 않습니다. 왜 이런 일이 발생합니까? 당신이 볼 수 있듯이 그 자체가 cdata가 아니고 나는 첫 번째 단계 만 가지고 있지만 foundCDATA를 사용하지 않을 때에도 나머지는 얻을 수 있습니다 : (NSData *) CDATABlock
이미지 링크를 가져 가고 싶습니다. 어떻게해야합니까? 나는 온라인으로 검색했고 많은 가이드가 오직 foundCDATA 함수를 사용한다고 설명했다. (NSData *) CDATABlock 그러나 어떻게 사용 되는가? 코드에서 사용한 방식은 무엇입니까?
이해해 주실 수 있도록 설명해주세요. 고마워요! 당신이 질문에 대한 대답에서
어떻게 다시 얻을 수 있습니까? 해당 이미지 소스에 대한 성문 표현 패턴 –