Objective-C-HMTL-Parser 0.0.1

Objective-C-HMTL-Parser 0.0.1

测试测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新的发布2014年12月

未申领维护。



  • Max Howell
  1. 在XCode中打开您的项目并将所有.h和.m文件拖放到适当的文件夹中
  2. 在项目设置中,将"/usr/include/libxml2"添加到"头文件搜索路径"字段
  3. 在Framework组中Ctrl Click选择添加 -> 现有Frameworks,并在列表中选择libxml2.dylib

示例用法

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];