parsing simple html for iphone

ぐ巨炮叔叔 提交于 2019-12-24 19:55:22

问题


I have a very simple html page to parse. The html page will remain simple always. as simple as this

<html>
<head><title>title</title></head>
<body>some data here</body>
</html>

I have fetched the html content of such an html page and have it in an NSString. I want to get what ever data is there in the body of the html page.

Please tell me how can this be done and let me know if there are more than one possible ways. I would prefer doing it using basic obj-c if it is possible.

Thanks


回答1:


If you control the content of the html page, you could simply use NSScanner to look for the tags you want. If you're looking for something that can handle more a complex set of tags, look into use NSXMLParser.




回答2:


Use NSScanner:

NSString *parsed;
NSScanner *scanner = [NSScanner scannerWithString: sourcecode];
[nscanner scanUpToString:@"<body>" intoString:NULL];
[nscanner scanUpToString:@"</body>" intoString: &parsed];
NSMutableString *finalparsed = [[NSMutableString alloc] initWithString: parsed];
[finalparsed deleteCharactersInRange:NSMakeRange(0, 6)];  //To get rid of <body>

finalparsed will contain only "some data here", or whatever is in between the body tags.



来源:https://stackoverflow.com/questions/2739670/parsing-simple-html-for-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!