0

有人可以帮我从这段代码中获取 href 属性吗?通过使用 NXMLParser .. 谢谢

"<content:encoded><![CDATA[<p><a class="lightbox"  title ="1" href="http://www.enlacejudio.com/wp-content/uploads/2012/05/12.jpg"><img src="http://www.enlacejudio.com/wp-content/uploads/2012/05/12.jpg" alt="" title="1" width="448" height="188" class="aligncenter size-full wp-image-59393" /></a></p>
4

1 回答 1

0

将其视为字符串,您可以使用 -

NSString *urlString = nil;
NSString *htmlString = //Your string;

NSScanner *scanner = [NSScanner scannerWithString:htmlString];

[scanner scanUpToString:@"href=" intoString:nil];
if (![scanner isAtEnd]) {
    [scanner scanUpToString:@"http" intoString:nil];
    NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@">"];
    [scanner scanUpToCharactersFromSet:charset intoString:&urlString];
}

编辑 -

-(id) loadXMLByURL:(NSString *)urlString { 
    noticias = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];

    NSStringEncoding encoding;
    NSString *xmlString = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error];
    parser = [[NSXMLParser alloc] initWithData:[xmlString dataUsingEncoding:encoding allowLossyConversion:YES]];
    parser.delegate = self; 
    [parser parse]; 
    return self;
} 
于 2012-05-16T17:04:40.250 回答