2012-05-16 127 views
0

有人可以幫我從這段代碼獲取href屬性嗎? 通過使用NXMLParser .. 三江源NSXML解析圖像

"<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> 

回答

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; 
} 
+0

對不起仙人,我忘了說,我我正在從互聯網上解析這一點,而我甚至無法閱讀內容:編碼部分。 – user1179587

+0

你得到這個作爲一些url命中的響應? – rishi

+0

不,我用nsxmlparser解析一個巨大的文檔,我能解析除了這部分以外的所有東西... – user1179587