IOS XMLParsing HTML标签进入描述字段

问题描述 投票:-1回答:1

我正在XMLparsing上,我成功获得了标题和其他描述,但是当我得到描述时,我也在描述字段中得到了html等,就像这样。如何从我的描述中删除它。在这里,我附上了我的xml解析方法。

我试图添加图像很多次,但是不知道我无法添加图像,所以我在这里添加了链接。

Fixed Link

这里是XML链接,我也从中获得HTML标记。

http://rss.news.yahoo.com/rss/world

//MainViewcontroller.h File
@interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDataSource,NSXMLParserDelegate>
{
NSXMLParser *parser;
NSMutableArray *feeds;
NSMutableDictionary *item;
NSMutableString *title;
NSMutableString *link;
NSString *element;
NSMutableString *CurrentAttribute;
NSMutableString *imageurlstring;
UIImage *image;
    NSMutableString *Description;

NSMutableArray *arrImage;
}
@property (weak, nonatomic) IBOutlet UITableView *tableview;

 //MainViewController.m File

 -(void)CallForWebService:(NSURL *)url
{
 parser = [[NSXMLParser alloc] initWithContentsOfURL:url];

 [parser setDelegate:self];
 arrImage = [[NSMutableArray alloc] init];

 imageurlstring = [[NSMutableString alloc]init];
 [parser setShouldResolveExternalEntities:NO];
 [parser parse];
 //[self.tableview reloadData];

  NSLog(@"%@",parser);
}




  - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

element = elementName;

if ([element isEqualToString:@"item"])
{
    item    = [[NSMutableDictionary alloc] init];
    title   = [[NSMutableString alloc] init];
    link    = [[NSMutableString alloc] init];
    imageurlstring=[[NSMutableString alloc]init];
    Description=[[NSMutableString alloc]init];


}
if([elementName isEqualToString:@"media:thumbnail"]) {
    NSLog(@"thumbnails media:thumbnail: %@", attributeDict);
    imageurlstring = [attributeDict objectForKey:@"url"];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

if ([element isEqualToString:@"title"])
{
    [title appendString:string];
} else if ([element isEqualToString:@"link"])
{
    [link appendString:string];
}
else if ([element isEqualToString:@"media:content"])
{
    [imageurlstring appendString:string];

}
 else if ([element isEqualToString:@"description"])
{
    [Description appendString:string];

}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if ([elementName isEqualToString:@"item"])
{
    [item setObject:title forKey:@"title"];
    [item setObject:link forKey:@"link"];
    [item setObject:imageurlstring forKey:@"media:content"];
    [item setObject:Description forKey:@"description"];

    [feeds addObject:[item copy]]; 
   //feed = nsarray and item = dictionaryname

}
- (void)parserDidEndDocument:(NSXMLParser *)parser 
{

[self.tableview reloadData];
}
ios xml-parsing
1个回答
1
投票

在github MWFeedParser上有一个非常不错的项目。请检查此项目,它有一些方法

- (NSString *)stringByStrippingTags;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities; 

而且您可以像这样在您的项目中使用该方法

NSString *summary = [[[myHtmlStringToDecode stringByStrippingTags] stringByRemovingNewLinesAndWhitespace] stringByDecodingHTMLEntities];

要解析图像,请检查MWFeedParser - RSS with images

© www.soinside.com 2019 - 2024. All rights reserved.