iphone Tutorial How to Parse HTML

Similar documents
Develop a ios Mobile App Consuming an OData Service Running in SAP HANA Cloud Platform

M, N, O F, G, H. network request, 101 ParseFacebookUtilities SDK, 100 profile, 100 user_about_me, 101 -(void)updateindicator, 101

ios App Development for Everyone

Praktikum Entwicklung von Mediensystemen mit ios

2. Create the User Interface: Open ViewController.xib or MainStoryBoard.storyboard by double clicking it.

Creating a Custom Class in Xcode

Send from your App Part 1

ios Dev Fest Research Network Operations Center Thursday, February 7, 13

ios App Development for Everyone

โปรแกรมบ นท ก ช อ และ อ เมล โดยจ ดเก บข อม ลลงไปท SQLite

BASIC IPHONE PROGRAMMING Case: Dictionary Application

Praktikum Entwicklung von Mediensystemen mit

Praktikum Entwicklung von Mediensystemen mit ios

Beginner level: Modules 1 to 18. Advanced level: Quick review of modules 1 to 18, then following to module A Simple ios Application

MA-WA1920: Enterprise iphone and ipad Programming

COMP327 Mobile Computing Session: Lecture Set 4 - Data Persistence, Core Data and Concurrency

EXPENSE TRACKER MOBILE APPLICATION. A Thesis. Presented to the. Faculty of. San Diego State University. In Partial Fulfillment

Harman Developer Documentation

Tutorial: ios OData Application Development with REST Services. Sybase Unwired Platform 2.2 SP04

INTRODUCTION TO IOS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 13 02/22/2011

Praktikum Entwicklung von Mediensystemen mit ios

ios Development Tutorial Nikhil Yadav CSE 40816/60816: Pervasive Health 09/09/2011

Your First ios Application

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Learn iphone and ipad game apps development using ios 6 SDK. Beginning. ios 6 Games. Development. Lucas Jordan. ClayWare Games tm

iphone Objective-C Exercises

Star Micronics Cloud Services ios SDK User's Manual

Assignment I Walkthrough

An Introduction to Modern Software Development Tools Creating A Simple GUI-Based Tool Appleʼs XCode Version 3.2.6

2. About iphone ios 5 Development Essentials. 5. Joining the Apple ios Developer Program

Implement continuous integration and delivery in your ios projects. Pro. ios Continuous Integration. Romain Pouclet.

ios App Performance Things to Take Care

Developing Applications for ios

Xcode Application note

IOS App Development Training

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Tag Specification Document

iphone ios 6 Development Essentials

Objective C and iphone App

Learning ios Programming

Introduction to Objective-C. Kevin Cathey

ios Dev Crib Sheet In the Shadow of C

ios App Programming Guide

ios App for Mobile Website! Documentation!

NSPersistentDocument Core Data Tutorial for Mac OS X v10.4. (Retired Document)

Personal Portfolios on Blackboard

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

Mobile Application Development

Adobe Dreamweaver CC 14 Tutorial

A MOBILE APPLICATION TO AID CONSUMERS IN ACCESSING COST EFFECTIVENESS IN THEIR AUTOMOBILE. A Thesis. Presented to the. Faculty of

Object Oriented Programming and the Objective-C Programming Language 1.0. (Retired Document)

Accessibility on ios. Make an app for everyone. Chris Fleizach ios Accessibility. Wednesday, December 1, 2010

Start Developing ios Apps Today

ios Application Development &

CyberSource ios SDK for Apple Pay

View Controller Programming Guide for ios

Development of an iphone business application

Jari Pohjanen CROSS PLATFORM APPLICATION DEVELOPMENT WITH HTML5 FOR IOS AND ANDROID OPERATING SYSTEMS

Developing an M-Learning Application for ios

17. November Übung 1 mit Swift. Architektur verteilter Anwendungen. Thorsten Kober Head Solutions ios/os X, SemVox GmbH

Contents. About Testing with Xcode 4. Quick Start 7. Testing Basics 23. Writing Test Classes and Methods 28. At a Glance 5 Prerequisites 6 See Also 6

Creating a Guest Book Using WebObjects Builder

Development with Modern Mobile Technologies

Starting User Guide 11/29/2011

BAM Checkout Mobile Implementation Guide for ios

Mobility Introduction Android. Duration 16 Working days Start Date 1 st Oct 2013

Softwareprojekt: Mobile Development Einführung Objective-C. Miao Wang, Tinosch Ganjineh Freie Universität Berlin, Institut für Informatik

ADOBE DREAMWEAVER CS3 TUTORIAL

Datatrans ios Payment Library

WebObjects Web Applications Programming Guide. (Legacy)

Poking a Hole in the Sandbox: Using URLs on ios

Your First Windows Mobile Application. General

Source Code Analysis for Security. Lu Zhao HP Fortify

Website Builder Overview

Design Document For DistroKon

Customize Mobile Apps with MicroStrategy SDK: Custom Security, Plugins, and Extensions

Introduction to iphone Development

We automatically generate the HTML for this as seen below. Provide the above components for the teaser.txt file.

Self Testing with MoPub SDK

TakeMySelfie ios App Documentation

MASTERTAG DEVELOPER GUIDE

From C++ to Objective-C

This documentation is made available before final release and is subject to change without notice and comes with no warranty express or implied.

Securing ios Applications. Dr. Bruce Sams, OPTIMAbit GmbH

Creating, Sharing, and Selling Buzztouch Plugins

Using the NPIN Customizable Testing and Treatment Service Referral Widget. Help Guide for Prevention Partners

TopBest Documentation Guide

Designing A Smart Phone Application With Xcode

Microsoft Tag Scanning SDK for iphone & Android Apps

Assignment 2: Matchismo 2

C-more Remote Access with Apple ipad or iphone Tutorial

MEAP Edition Manning Early Access Program Hello! ios Development version 14

My IC Customizer: Descriptors of Skins and Webapps for third party User Guide

PE Content and Methods Create a Website Portfolio using MS Word

5.7. Quick Guide to Fusion Pro Schedule

Database Forms and Reports Tutorial

Mobile Client SDK for ios Developer's Guide

Centrify Mobile Authentication Services

Ruby and Python Programming Topics for Mac. (Retired Document)

Communication Manager Template Library

Transcription:

iphone Tutorial How to Parse HTML In this tutorial we are going to parse a webpage and put the results into a list. The html page we are parsing is a very simple and you can view it at http://www.bhecker.com/testpage.html. 1. Start Xcode, Select Create a new Xcode project, and choose the Single View Application template. Click next, name the project ParseHtml, and select options as shown:

Click Next, choose a location to save the project and click Create. 2. We will first create an object to hold the html data. We will name this class HtmlElement and it will just be a simple Objective-C class that is a subclass of NSObject. Select File->New->File and then Objective-C class.

3. Our new object will hold two NSStrings one for the name of the html tag and one for the value of the tag. Open HtmlElement.h and add tow properties as follows: @property (nonatomic, retain) NSString *tag; @property (nonatomic, retain) NSString *value; 4. The only thing we do in the implementation file is synthesize our variables. Open HtmlElement.m and add the following line of code. @synthesize tag, value; 5. Next we need to create a parser class. Name it HtmlParser and it should also be a simple class that is a subclass of NSObject. Select File->New->File and then Objective-C class. Name the class HtmlParser. The parser object needs to import the HtmlElement.h class, hold four instance variables and declares one action. It also needs to implement NSXMLParserDelegate protocol. Open HtmlParser.h and make the following changes: Add the line to import the element: #import "HtmlElement.h" Add <NSXMLParserDelegate> to the interface line to use the protocol in the class implementation. @interface HtmlParser : NSObject <NSXMLParserDelegate> Add the following 4 properties and 1 method prototype:

@property NSMutableString *currentnodecontent; @property NSMutableArray *elementarray; @property NSXMLParser *parser; @property HtmlElement *currenthtmlelement; -(id) loadhtmlbyurl:(nsstring *)urlstring; 5. Open HtmlParser.m and synthesize the properties we have created as follows: @synthesize currenthtmlelement, elementarray, parser, currentnodecontent; 6. We also need to add the implementation for the loadhtmlbyurl method. -(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; 7. Next we need to implement 3 methods to follow the NSXMLParserDelegate protocol.

- (void) parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname attributes:(nsdictionary *)attributedict currenthtmlelement = [HtmlElement alloc]; - (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]]; 8. The rest of the work is done in the ViewController. The header file imports the HtmlParser object and declares it as an instance variable. Open ViewController.h and add the line to import HtmlParsser.h as follows: #import "HtmlParser.h" Make the ViewContoller a subclass of UITableViewController instead of UIViewController by making this change: @interface ViewController : UITableViewController Make a new property for an instance of an HtmlParser as follows: @property HtmlParser *htmlparser; 9. Now, open ViewController.m and add the line to synthesize as follows:

@synthesize htmlparser; 10. In the viewdidload method alloc the htmlparser and pass the url to the html page into the loadhtmlbyurl action as follows: - (void)viewdidload [super viewdidload]; htmlparser = [[HtmlParser alloc] loadhtmlbyurl:@"http://www.bhecker.com/testpage.html"] ; self.title = @"HTML Elements"; 11. Add a new method, TableView and set the numberofrowsinsection method to use the elementarray by adding the following: // Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableview numberofrowsinsection:(nsinteger)section return [[htmlparser elementarray] count]; 12. Everything else gets done in the cellforrowatindexpath method. We set the current HtmlElement from the elementarray and then use it to set the labels in the cells. // Customize the appearance of table view cells. - (UITableViewCell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil)

cell = [[UITableViewCell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; // Configure the cell. HtmlElement *currentelement = [[htmlparser elementarray] objectatindex:indexpath.row]; cell.textlabel.text = [currentelement tag]; cell.detailtextlabel.text = [currentelement value]; return cell; 13. The last step is to make sure you are using the correct nib type for the project type. Select the ViewController.xib file and then view the properties in the attribute inspector. Change the class (3 rd icon from the left) from UIView to UITableView. Don t put anything in the interface, it will be populated programmatically. You can change the background color if you wish. 13. Build and run and see the results: