【发布时间】:2011-08-02 03:37:10
【问题描述】:
NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>";
现在,我想得到pic.jpg,
如何搜索字符串?
【问题讨论】:
标签: html objective-c iphone tags
NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>";
现在,我想得到pic.jpg,
如何搜索字符串?
【问题讨论】:
标签: html objective-c iphone tags
代码依赖于libxml2 并且基本上是一个瘦包装器,它为使用Objective C 解析html 提供了一个更方便的接口。这仅在iOS 3.1.3 和3.2 上进行了测试,如果你使用的是OSX,你'重新调查使用 WebKit 来操纵你的 DOM 可能会更好。以下代码可能会对您有所帮助。
//Example to download google's source and print out the urls of all the images
NSError * error = nil;
HTMLParser * parser = [[HTMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"] error:&error];
if (error) {
NSLog(@"Error: %@", error);
return;
}
HTMLNode * bodyNode = [parser body]; //Find the body tag
NSArray * imageNodes = [bodyNode findChildTags:@"img"]; //Get all the <img alt="" />
for (HTMLNode * imageNode in imageNodes) { //Loop through all the tags
NSLog(@"Found image with src: %@", [imageNode getAttributeNamed:@"src"]); //Echo the src=""
}
[parser release];
【讨论】: