forked from spawrks/Objective-C-HMTL-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLParser.m
130 lines (102 loc) · 2.38 KB
/
HTMLParser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// HTMLParser.m
// StackOverflow
//
// Created by Ben Reeves on 09/03/2010.
// Copyright 2010 Ben Reeves. All rights reserved.
//
#import "HTMLParser.h"
@implementation HTMLParser
-(HTMLNode*)doc
{
if (_doc == NULL)
return NULL;
return [[HTMLNode alloc] initWithXMLNode:(xmlNode*)_doc];
}
-(HTMLNode*)html
{
if (_doc == NULL)
return NULL;
return [[self doc] findChildTag:@"html"];
}
-(HTMLNode*)head
{
if (_doc == NULL)
return NULL;
return [[self doc] findChildTag:@"head"];
}
-(HTMLNode*)body
{
if (_doc == NULL)
return NULL;
return [[self doc] findChildTag:@"body"];
}
-(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);
// _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;
}
-(id)initWithData:(NSData*)data error:(NSError**)error
{
if (self = [super init])
{
_doc = NULL;
if (data)
{
CFStringEncoding cfenc = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);
CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);
const char *enc = CFStringGetCStringPtr(cfencstr, 0);
//_doc = htmlParseDoc((xmlChar*)[data bytes], enc);
_doc = htmlReadDoc((xmlChar*)[data bytes],
"",
enc,
XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
}
else
{
if (error)
{
*error = [NSError errorWithDomain:@"HTMLParserdomain" code:1 userInfo:nil];
}
}
}
return self;
}
-(id)initWithContentsOfURL:(NSURL*)url error:(NSError**)error
{
NSData * _data = [[NSData alloc] initWithContentsOfURL:url options:0 error:error];
if (_data == nil || *error)
{
return nil;
}
self = [self initWithData:_data error:error];
return self;
}
-(void)dealloc
{
if (_doc)
{
xmlFreeDoc(_doc);
}
}
@end