1

How do I parse an html file?

I need the data in between span tag.

<div id=currency_converter_result>1 AED = <span class=bld>0.4765 ANG</span>
0

3 Answers 3

2

If you need only tag 'span' you can use NSRegularExpression such as this one

NSString *html = @"<div id=currency_converter_result>1 AED = <span class=bld>0.4765 ANG</span>";
NSRegularExpression *regex = [NSRegularExpression
                               regularExpressionWithPattern:@"<span[^>]*>(.+?)</span>"
                               options:NSRegularExpressionCaseInsensitive
                               error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:html options:0 range:NSMakeRange(0, html.length)];
NSLog(@"found: '%@'", [html substringWithRange:[textCheckingResult rangeAtIndex:1]]);

or, I prefer to create a NSDictionary from xml or html data and work with it. You can do it with XML-to-NSDictionary library.

1
  • If you don't need to step through the structure, this is a great way to do it IMO.
    – LJ Wilson
    Jan 21, 2012 at 18:30
1

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

Basic Usage :

NSError *error = nil;
NSString *htmlString = 
    @"<div id=currency_converter_result>1 AED = <span class=bld>0.4765 ANG</span>";
HTMLParser *p = [[HTMLParser alloc] initWithString:htmlString error:&error];

if (error) {
    NSLog(@"Error: %@", error);
    return;
}

HTMLNode *bodyNode = [p body];

NSArray *spanNodes = [bodyNode findChildTags:@"span"];

for (HTMLNode *spanNode in spanNodes) {
    if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"bld"]) {
        NSLog(@"%@", [spanNode rawContents]); //Answer to second question
    }
}
1

Since XHTML is XML, you can consider using NSXMLParser:

@interface HTMLParser: NSObject <NSXMLParserDelegate> // or whichever superclass you have
{
    // own declarations
    NSMutableString *str;
    NSXMLParser *parser;
}

// somewhere in a method of self, for example, init, or something named -(void) parseHtml
- (void) parseHtml
{
    parser = [[NSXMLParser alloc] initWithData:[@"<div id=currency_converter_result>1 AED = <span class=bld>0.4765 ANG</span>" dataUsingEncoding:NSUTF8StringEncoding]];
    // of course you can substitute any string you want here, for example, the result of [NSString stringWithContentsOfFile:@"inex.html"] or whatever you need.
    parser.delegate = self;
    [parser parse];
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"span"] && [[attributeDict objectForKey:@"class"] isEqualToString:@"bld"])
    {
        str = [NSMutableString string];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [str appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"span"])
    {
        // now str contains the value you want!
        DoSomethingWith(str);
    }
}

Hope this helps.

3
  • I know this is old but I just came across this. HTML is NOT XML. xHTML is, but general HTML is definitely not valid XML. XML requires a close tag for every open tag. XML requires proper tag nesting. XML requires all attribute values be in quotes. HTML requires none of this.
    – rmaddy
    Nov 10, 2012 at 19:27
  • @maddy Yes, that's right, but who cares about non-conformant code? At least I don't. If I were to make a website, I'd use XHTML only.
    – user529758
    Nov 10, 2012 at 19:40
  • You made a false claim that HTML is XML. I'm simply pointing that out. Yes, using xHTML is a good idea if you have the chance. But many people have a need to parse HTML that is beyond their control or HTML that isn't xHTML. Many people need to worry about "non-conformant" HTML code. It's a fact of life.
    – rmaddy
    Nov 10, 2012 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.