Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-17-2009, 02:03 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 27
ciri is on a distinguished road
Default Parsing HTML file

Hi everybody,

I parse an html file with NSXMLDocument object:
Code:
document = [[NSXMLDocument alloc] initWithData:receivedData options:NSXMLDocumentTidyHTML error:&errorXML];
		NSXMLElement *rootNode = [document rootElement];
		NSString *xpathQueryString = @"//a[@class='blu10_link']";
		NSArray *newItemsNodes = [rootNode nodesForXPath:xpathQueryString error:&errorXML];
		[self willChangeValueForKey:@"newItems"];
		
		[newItems release];
		[newItemsURL release];
		newItems = [[NSMutableArray array] retain];
		newItemsURL = [[NSMutableArray array] retain];
		for(NSXMLElement *node in newItemsNodes){
			NSString *relativeString = [[node attributeForName:@"title"] stringValue];
			NSString *relativeStringURL = [[node attributeForName:@"href"] stringValue];
			[newItems addObject:relativeString];	
			[newItemsURL addObject:relativeStringURL];
		}
And I work fine with this, but there are parts of html file that are enclosed in a html tag without some attributes. For example:
Code:
<table>
<tr>
<td> Some text </td>
</tr>
</table>
How can I take the text in this cases?

Thank you and sorry for my bad english but I'm italian
ciri is offline   Reply With Quote
Old 02-17-2009, 02:23 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
lbendlin is on a distinguished road
Default

each XML node has a ".text" property which shoud give you what you need.
lbendlin is offline   Reply With Quote
Old 02-18-2009, 12:00 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 27
ciri is on a distinguished road
Default

Sorry but i don't see any .text property

If you can, write me a little example to help me to understand.

Thanks.
ciri is offline   Reply With Quote
Old 02-18-2009, 12:08 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by ciri View Post
Sorry but i don't see any .text property

If you can, write me a little example to help me to understand.

Thanks.
Check out XMLParser example app from Apple. It will show you how to achieve this.
wuf810 is offline   Reply With Quote
Old 02-18-2009, 12:45 PM   #5 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 178
dustbyter is on a distinguished road
Default

Quote:
Originally Posted by wuf810 View Post
Check out XMLParser example app from Apple. It will show you how to achieve this.
Remember there are two ways to parse an XML file.

1. Use the a DOM tree approach which is what you are trying todo
2. Use a streaming API like the example shown in the apple tutorial.

Try the streaming api as in the apple example, its not hard at all to set up.

Basically, its a set up to have events triggered when certain events occur in the xml file.
dustbyter is offline   Reply With Quote
Old 02-18-2009, 02:45 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 27
ciri is on a distinguished road
Default

Ok, with the method

Code:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
I can get the characters that aren't into the HTML tags.

But now I have a NSMutableString with a lot of characters. How do I take only the text I want?

Thanks.
ciri is offline   Reply With Quote
Old 02-18-2009, 03:02 PM   #7 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 178
dustbyter is on a distinguished road
Default

Don't remember the method off hand, but there is an method that is ran when the endElement is reached.

At this point you have all the text from current element being processed.

It's up to you to pull just what you need.

For example if the tag you processed had the text:

Hello World...I mean Universe

If you wanted to take a portion of that string you need to pull it manually using the string manipulation API available to you.
dustbyter is offline   Reply With Quote
Old 02-19-2009, 06:33 AM   #8 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 27
ciri is on a distinguished road
Default

Quote:
Originally Posted by dustbyter View Post
For example if the tag you processed had the text:

Hello World...I mean Universe
But I don't know how the string begins.

I know that the text is into a table without specific attributes.

For example:
Code:
<table colspan="2">
<tr>
<td>
the text that interests me
</td>
</table>
ciri is offline   Reply With Quote
Old 02-19-2009, 06:43 AM   #9 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by ciri View Post
But I don't know how the string begins.

I know that the text is into a table without specific attributes.

For example:
Code:
<table colspan="2">
<tr>
<td>
the text that interests me
</td>
</table>
ciri.

I'm sorry to say this but you have just got to give it a try. You've been given enough information about how to progress...so go and give it a go.

If then you have further questions/issues, you can post your code and we can help.

Regards, M.
wuf810 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 330
4 members and 326 guests
guusleijsten, HowEver, LEARN2MAKE, mottdog
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:42 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0