Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 解析RSS后如何删除XML标记中的HTML标记?_Objective C_Xml_Parsing - Fatal编程技术网

Objective c 解析RSS后如何删除XML标记中的HTML标记?

Objective c 解析RSS后如何删除XML标记中的HTML标记?,objective-c,xml,parsing,Objective C,Xml,Parsing,大家好,我有个问题 目前,我能够解析新闻web上的RSS XML,并将其显示在UITableViewCell中。我解析描述标记,它是: <description><![CDATA[ <a href="http://vnexpress.net/gl/xa-hoi/2012/05/thuy-dien-song-tranh-de-bi-lo-sut-dat-1/"><img src="http://vnexpress.net/Files/Subject/3b/bd/

大家好,我有个问题

目前,我能够解析新闻web上的RSS XML,并将其显示在UITableViewCell中。我解析描述标记,它是:

<description><![CDATA[ <a href="http://vnexpress.net/gl/xa-hoi/2012/05/thuy-dien-song-tranh-de-bi-lo-sut-dat-1/"><img src="http://vnexpress.net/Files/Subject/3b/bd/66/e0/sat-lo-dat-Song-Tranh-2.jpg"></a>this is new<BR />></description>
<![CDATA[ <a href="http://vnexpress.net/gl/xa-hoi/2012/05/thuy-dien-song-tranh-de-bi-lo-sut-dat-1/"><img src="http://vnexpress.net/Files/Subject/3b/bd/66/e0/sat-lo-dat-Song-Tranh-2.jpg"></a>this is new<BR />>
我还想得到这个描述标签中的图像,这样我就可以显示它了:

<img src="http://vnexpress.net/Files/Subject/3b/bd/66/e0/sat-lo-dat-Song-Tranh-2.jpg">
。请告诉我怎么做?提前感谢。

对于iOS 7+您可以使用NSAttributedString,如下所示:

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];
对于低于iOS 7的版本,请使用此代码删除<和>

(NSString *) stringByStrippingHTML {
  NSRange r;
  NSString *s = [[self copy] autorelease];
  while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    s = [s stringByReplacingCharactersInRange:r withString:@""];
  return s;
}

我以前必须这样做。因此,我将粘贴我在这里使用的代码

- (NSString *)removeHTMLTags:(NSString *)str
{   
NSMutableString *temp_str = [[NSMutableString alloc] initWithString:str];
NSRange openTag = [temp_str rangeOfString:@"<"];
NSRange closeTag = [temp_str rangeOfString:@">"];

while (openTag.length > 0) {
    NSRange range;
    range.location = openTag.location;
    range.length = (closeTag.location - openTag.location) + 1;
    [temp_str setString:[temp_str stringByReplacingCharactersInRange:range withString:@""]];

    openTag = [temp_str rangeOfString:@"<"];
    closeTag = [temp_str rangeOfString:@">"];
}

[temp_str replaceOccurrencesOfString:@"&Auml;" withString:@"Ä" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[temp_str replaceOccurrencesOfString:@"&Aring;" withString:@"Å" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[temp_str replaceOccurrencesOfString:@"&AElig;" withString:@"Æ" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];


while ([temp_str rangeOfString:@"  "].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@"  " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}

while ([temp_str rangeOfString:@" ."].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@" ." withString:@"." options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}

while ([temp_str rangeOfString:@" ,"].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@" ," withString:@"," options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}

while ([temp_str rangeOfString:@" ;"].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@" ;" withString:@";" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}


return temp_str;
}

谢谢您的回答,顺便问一下,您知道如何从HTML标签中获取图像,如下所示:
- (NSString *)removeHTMLTags:(NSString *)str
{   
NSMutableString *temp_str = [[NSMutableString alloc] initWithString:str];
NSRange openTag = [temp_str rangeOfString:@"<"];
NSRange closeTag = [temp_str rangeOfString:@">"];

while (openTag.length > 0) {
    NSRange range;
    range.location = openTag.location;
    range.length = (closeTag.location - openTag.location) + 1;
    [temp_str setString:[temp_str stringByReplacingCharactersInRange:range withString:@""]];

    openTag = [temp_str rangeOfString:@"<"];
    closeTag = [temp_str rangeOfString:@">"];
}

[temp_str replaceOccurrencesOfString:@"&Auml;" withString:@"Ä" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[temp_str replaceOccurrencesOfString:@"&Aring;" withString:@"Å" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[temp_str replaceOccurrencesOfString:@"&AElig;" withString:@"Æ" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];


while ([temp_str rangeOfString:@"  "].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@"  " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}

while ([temp_str rangeOfString:@" ."].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@" ." withString:@"." options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}

while ([temp_str rangeOfString:@" ,"].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@" ," withString:@"," options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}

while ([temp_str rangeOfString:@" ;"].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@" ;" withString:@";" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}


return temp_str;
}