About
Home > Tutorials Home > Ios > HTML Parsing Demo in iOS

HTML Parsing Demo in iOS

Published on October 17, 2019 8:24:05 PM

Button Example

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.This is the ButtonPressed example. In this example we will see how to load another view after pressing button. So let see how it will worked. Another Buttonpress reference you can find out from here ButtonView in iPhone.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the XCode and create a new Single View Based Application template. Give the application name "HTMLParsing Example". Add a new .h File Name it HTMLElement.h

#import <Foundation/Foundation.h>

@interface HtmlElement : NSObject
{
	NSString *tag;
	NSString *value;
}

@property (nonatomic, retain) NSString *tag;
@property (nonatomic, retain) NSString *value;

@end


Step 2 : Copy the Following Code into your HTMLElement.m File.

#import "HtmlElement.h"

@implementation HtmlElement

@synthesize	tag, value;

@end

Step 3 : Add another .h file to your Project and Name it HtmlParser.h

#import <Foundation/Foundation.h>

#import "HtmlElement.h"

@vinterface HtmlParser : NSObject <NSXMLParserDelegate>
{
    NSMutableString *currentNodeContent;
    NSMutableArray  *elementArray;
    NSXMLParser *parser;
    HtmlElement *currentHTMLElement;
}

@property (readonly, retain) NSMutableArray *elementArray;

-(id) loadHtmlByURL:(NSString *)urlString;

@end

Step 4 : Copy the below Code to the HTMLParser.m File.

#import "HtmlParser.h"
#import "HtmlElement.h"

@implementation HtmlParser

@synthesize elementArray;

-(id) loadHtmlByURL:(NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
	NSData *nsData = [[NSData alloc] initWithContentsOfURL:url];
    elementArray = [[NSMutableArray alloc] init];
	parser = [[NSXMLParser alloc] initWithData:nsData];
	parser.delegate = self;
	[parser parse];
	currentHTMLElement = [HtmlElement alloc];
    return self;
}

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	currentHTMLElement = [[HtmlElement alloc] autorelease];
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementname isEqualToString:@"title"])
    {
		currentHTMLElement.tag = elementname;
		currentHTMLElement.value = currentNodeContent;
		[elementArray addObject:currentHTMLElement];
		currentHTMLElement = nil;
		currentNodeContent = nil;
	}
    if ([elementname isEqualToString:@"h1"])
    {
		currentHTMLElement.tag = elementname;
		currentHTMLElement.value = currentNodeContent;
		[elementArray addObject:currentHTMLElement];
		currentHTMLElement = nil;
		currentNodeContent = nil;
	}
    if ([elementname isEqualToString:@"p"])
	{
		currentHTMLElement.tag = elementname;
		currentHTMLElement.value = currentNodeContent;
		[elementArray addObject:currentHTMLElement];
		currentHTMLElement = nil;
		currentNodeContent = nil;
	}
    if ([elementname isEqualToString:@"div"])
	{
		currentHTMLElement.tag = elementname;
		currentHTMLElement.value = currentNodeContent;
		[elementArray addObject:currentHTMLElement];
		currentHTMLElement = nil;
		currentNodeContent = nil;
	}
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (void) dealloc
{
    [parser release];
	[elementArray release];
	[super dealloc];
}

@end
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile