Safari
Safari

Reputation: 11965

Extract an html tag and attribute from NSString

I have a NSString that contains a HTML page. I would to extract a particular tag by id and a particular attribute (of this tag) by name. I used regular expression as this example:

NSError *error = nil;
    NSString *authParameter = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<form id=\"theIdOfForm\"*.*method=\"post\">" options:NSRegularExpressionCaseInsensitive error:&error];

    if (error == nil) {
        NSTextCheckingResult *match = [regex firstMatchInString:aHtmlFirstResponse options:0 range:NSMakeRange(0, [aHtmlFirstResponse length])];

        if (match) {
            NSString *formHtmlTag = [aHtmlFirstResponse substringWithRange:[match rangeAtIndex:0]];

}

but, how can I extract the attribute by name from this tag? What is an elegant and efficient way to obtains this? I would like to avoid importing some HTML parser library.

Upvotes: 0

Views: 711

Answers (1)

user2071152
user2071152

Reputation: 1195

You could use

https://github.com/zootreeves/Objective-C-HMTL-Parser

I have used this for one of my project.

Upvotes: 1

Related Questions