0
NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>";
現在,我想pic.jpg
,我如何解析html代碼!我想獲得的HTML代碼標記爲iPhone
如何搜索字符串?
NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>";
現在,我想pic.jpg
,我如何解析html代碼!我想獲得的HTML代碼標記爲iPhone
如何搜索字符串?
該代碼取決於libxml2
,基本上是一個薄包裝,它提供了一個更方便的接口來解析HTML與目標C.這隻在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];
哇.. 這就是我想要的結果 感謝您的幫助,HonestSuccess。 你能幫助我嗎? 如果我還沒有libxml2框架。 我該怎麼做,並得到結果? – rbbtsn0w
好的..謝謝..我只是怎麼做到的 – rbbtsn0w