一個輕量級的 Objective-C-HMTL-Parser 解析庫,這裡主要是希望 HTML 解析出想要的標籤欄位。
題外:如果是要將 HTML 解析成富文字展示,推薦 DTCoreText。
新增到 framework 或 app 在 Build Setting 的 Header Search Paths 新增 $(SDKROOT)/usr/include/libxml2。
如果是新增到 podspec 則新增,則:
s.library = 'xml2'
s.xcconfig = { 'HEADER_SEARCH_PATHS' => '$(SDKROOT)/usr/include/libxml2', 'OTHER_LDFLAGS' => '-lxml2' }
複製程式碼
解決 libxml2 中文亂碼
/**
解決 xml2 中文亂碼
參考:https://blog.fedepot.com/ios-9-htmlparserzhong-wen-luan-ma-wen-ti/
*/
-(id)initWithString:(NSString*)string error:(NSError**)error
{
if (self = [super init])
{
_doc = NULL;
if ([string length] > 0)
{
CFStringEncoding cfenc = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);
CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);
const char *enc = CFStringGetCStringPtr(cfencstr, 0);
//Fix iOS9 Chinese wrong characters - begin
char buffer[255];
if (enc == NULL) {
if (CFStringGetCString(cfencstr, buffer, 255, kCFStringEncodingUTF8)) enc = buffer;
}
//Fix iOS9 Chinese wrong characters - end
// _doc = htmlParseDoc((xmlChar*)[string UTF8String], enc);
int optionsHtml = HTML_PARSE_RECOVER;
optionsHtml = optionsHtml | HTML_PARSE_NOERROR; //Uncomment this to see HTML errors
optionsHtml = optionsHtml | HTML_PARSE_NOWARNING;
_doc = htmlReadDoc ((xmlChar*)[string UTF8String], NULL, enc, optionsHtml);
}
else
{
if (error) {
*error = [NSError errorWithDomain:@"HTMLParserdomain" code:1 userInfo:nil];
}
}
}
return self;
}
複製程式碼