Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[HTMLParser head] #9

Merged
merged 4 commits into from

2 participants

Max Howell Ben Reeves
Max Howell

You don't have to pull this, but personally I wanted it to make my code prettier.

The README addition is probably a no brainer though.

Ben Reeves zootreeves merged commit 172a033 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 20, 2011
  1. Max Howell

    [HTMLParser head] for convenience

    mxcl authored
  2. Max Howell

    Include example in README

    mxcl authored
    Convert formatting to Markdown.
Commits on Oct 16, 2011
  1. Max Howell
  2. Max Howell
This page is out of date. Refresh to see the latest.
Showing with 55 additions and 6 deletions.
  1. +3 −0  HTMLParser.h
  2. +10 −3 HTMLParser.m
  3. +0 −3  README
  4. +42 −0 README.md
3  HTMLParser.h
View
@@ -31,4 +31,7 @@
//Returns the html tag
-(HTMLNode*)html;
+//Returns the head tag
+- (HTMLNode*)head;
+
@end
13 HTMLParser.m
View
@@ -27,6 +27,14 @@ -(HTMLNode*)html
return [[self doc] findChildTag:@"html"];
}
+-(HTMLNode*)head
+{
+ if (_doc == NULL)
+ return NULL;
+
+ return [[self doc] findChildTag:@"head"];
+}
+
-(HTMLNode*)body
{
if (_doc == NULL)
@@ -47,8 +55,7 @@ -(id)initWithString:(NSString*)string error:(NSError**)error
CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);
const char *enc = CFStringGetCStringPtr(cfencstr, 0);
// _doc = htmlParseDoc((xmlChar*)[string UTF8String], enc);
- int optionsHtml = 0;
- optionsHtml = optionsHtml | HTML_PARSE_RECOVER;
+ 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);
@@ -106,7 +113,7 @@ -(id)initWithContentsOfURL:(NSURL*)url error:(NSError**)error
return nil;
}
- [self initWithData:_data error:error];
+ self = [self initWithData:_data error:error];
[_data release];
3  README
View
@@ -1,3 +0,0 @@
-1. Open Your project in XCode and drag and drop all .h & .m Files into an appropriate folder
-2. In the project settings add "/usr/include/libxml2" to the "header search paths" field
-3. Ctrl Click the Frameworks group choose Add -> Existing Frameworks and from the list choose libxml2.dylib
42 README.md
View
@@ -0,0 +1,42 @@
+1. Open Your project in XCode and drag and drop all .h & .m Files into an appropriate folder
+2. In the project settings add "/usr/include/libxml2" to the "header search paths" field
+3. Ctrl Click the Frameworks group choose Add -> Existing Frameworks and from the list choose libxml2.dylib
+
+Example Usage
+=============
+```objc
+NSError *error = nil;
+NSString *html =
+ @"<ul>"
+ "<li><input type='image' name='input1' value='string1value' /></li>"
+ "<li><input type='image' name='input2' value='string2value' /></li>"
+ "</ul>"
+ "<span class='spantext'><b>Hello World 1</b></span>"
+ "<span class='spantext'><b>Hello World 2</b></span>";
+HTMLParser *parser = [[HTMLParser alloc] initWithString:html error:&error];
+
+if (error) {
+ NSLog(@"Error: %@", error);
+ return;
+}
+
+HTMLNode *bodyNode = [parser body];
+
+NSArray *inputNodes = [bodyNode findChildTags:@"input"];
+
+for (HTMLNode *inputNode in inputNodes) {
+ if ([[inputNode getAttributeNamed:@"name"] isEqualToString:@"input2"]) {
+ NSLog(@"%@", [inputNode getAttributeNamed:@"value"]); //Answer to first question
+ }
+}
+
+NSArray *spanNodes = [bodyNode findChildTags:@"span"];
+
+for (HTMLNode *spanNode in spanNodes) {
+ if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"spantext"]) {
+ NSLog(@"%@", [spanNode rawContents]); //Answer to second question
+ }
+}
+
+[parser release];
+```
Something went wrong with that request. Please try again.