1

こんにちは、RSS の解析に関する質問があります。

現在、ニュース 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 />>

プレーンテキストを表示したいだけです:

this is new

また、この説明タグで画像を取得して、表示できるようにします。

<img src="http://vnexpress.net/Files/Subject/3b/bd/66/e0/sat-lo-dat-Song-Tranh-2.jpg">

. 方法を教えてください。前もって感謝します。

4

2 に答える 2

1

私は前にこれをしなければなりませんでした。ここで使用したコードを貼り付けます。

- (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;
}
于 2012-05-08T04:25:22.383 に答える
-1

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;
}
于 2012-05-08T04:16:18.113 に答える