Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.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 06-24-2009, 07:09 AM   #1 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 5
Default Parsing a local XML file, importing a file

Hello all,

I'd like to be able to parse a local XML file, rather than one hosted out on the web somewhere. I followed the example found here: Parsing XML Files - iPhone SDK Articles but was unsuccessful in modifying the project to read a local file. Any help would be greatly appreciated.

Once I get that working, I'd like to be able to import XML files into an app. Preferably by emailing the file and opening the attachment on the iPhone. Is this doable, and if so, are there tutorials and/or documentation that someone can point out?

Thanks in advance!
Miles is offline   Reply With Quote
Old 06-24-2009, 09:01 AM   #2 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 6
Default

Quote:
Originally Posted by Miles View Post
Hello all,

I'd like to be able to parse a local XML file, rather than one hosted out on the web somewhere. I followed the example found here: Parsing XML Files - iPhone SDK Articles but was unsuccessful in modifying the project to read a local file. Any help would be greatly appreciated.

Once I get that working, I'd like to be able to import XML files into an app. Preferably by emailing the file and opening the attachment on the iPhone. Is this doable, and if so, are there tutorials and/or documentation that someone can point out?

Thanks in advance!
Try this:

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData datawithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.xml"]]]

That will take the contents of the XML file 'test.xml' in the local bundle, and load it as an NSXMLParser object.

Alternatively, to take a file from the Applications 'Documents' directory, use the following to set the path:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory,NSUserDomainMask,YES);
NSString *xmlPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"test.xml"];


Hope this helps.

Seidr

Last edited by Seidr; 06-24-2009 at 09:04 AM.
Seidr is offline   Reply With Quote
Old 06-24-2009, 01:47 PM   #3 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 5
Default

Quote:
Originally Posted by Seidr View Post
Try this:
Thanks for the reply!

In the project I linked to, I altered XMLAppDelegate.m by replacing the lines:

Code:
	NSURL *url = [[NSURL alloc] initWithString:@"http://sites.google.com/site/iphonesdktutorials/xml/Books.xml"];
	NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
with:

Code:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData datawithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.xml"]]]
When attempted to Build and Go, I received the warning:

Code:
.../XML/Classes/XMLAppDelegate.m:22: warning: 'NSData' may not respond to '+datawithContentsOfFile:'
and errors on the following lines:

Code:
	XMLParser *parser = [[XMLParser alloc] initXMLParser];
error: expected ',' or ';' before 'XMLParser'
Code:
	[xmlParser setDelegate:parser];
error: 'XMLParser' undelcared (first use in this function)
As you might guess, I am a newbie developer, and I would appreciate any guidance anyone could provide.

Thanks again!
Miles is offline   Reply With Quote
Old 06-24-2009, 02:00 PM   #4 (permalink)
Magic Hands' Daddy
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 22
Posts: 1,371
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Quote:
Originally Posted by Miles View Post
Thanks for the reply!

In the project I linked to, I altered XMLAppDelegate.m by replacing the lines:

Code:
	NSURL *url = [[NSURL alloc] initWithString:@"http://sites.google.com/site/iphonesdktutorials/xml/Books.xml"];
	NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
with:

Code:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData datawithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.xml"]]]
When attempted to Build and Go, I received the warning:

Code:
.../XML/Classes/XMLAppDelegate.m:22: warning: 'NSData' may not respond to '+datawithContentsOfFile:'
and errors on the following lines:

Code:
	XMLParser *parser = [[XMLParser alloc] initXMLParser];
error: expected ',' or ';' before 'XMLParser'
Code:
	[xmlParser setDelegate:parser];
error: 'XMLParser' undelcared (first use in this function)
As you might guess, I am a newbie developer, and I would appreciate any guidance anyone could provide.

Thanks again!
Capitalization problems...

Code:
datawithContentsOfFile
should be
Code:
dataWithContentsOfFile

and it sshould be NSXMLParser
__________________
Games
AppRoach - (v1.0)
7 Deaths In Nagamachi (v1.1)
Segment (v1.2)
ThumStruck Free (v1.5)
Plummet - new (v1.0)
ThumStruck 2! - Coming Soon

Apps
ArtsMemphis - (v1.0)
Elvis Mobile (v1.1)
DataLoss DB - (v1.1)
Lovers & Haters - (v1.0)
smithdale87 is offline   Reply With Quote
Old 06-24-2009, 02:14 PM   #5 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 500
Default

Quote:
Originally Posted by Miles View Post
Hello all,

I'd like to be able to parse a local XML file, rather than one hosted out on the web somewhere.
You might want to take a look at the iPhone in Action book and source code. They have an example in Chapter 20 that reads an XML file stored on the device.
DenVog is offline   Reply With Quote
Old 06-24-2009, 02:22 PM   #6 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 5
Default

Quote:
Originally Posted by smithdale87 View Post
Capitalization problems...
Thank you. Fixing dataWithContentsOfFile got rid of that warning.

Quote:
Originally Posted by smithdale87
and it should be NSXMLParser
Where should I change this?

I tried changing each of the three instances of "xmlparser" to "NSXMLParser" in the first bad line, individually and in combination, and the error did not change.

If I change "xmlParser" to "NSXMLParser" in the second bad line, the error changes to the warning:
Code:
.../iPhone Apps/XML/Classes/XMLAppDelegate.m:28: warning: 'NSXMLParser' may not respond to '+setDelegate:'
Thanks again for the advice.
Miles is offline   Reply With Quote
Old 06-24-2009, 02:48 PM   #7 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 92
Default

He's talking about your class XMLParser. Is this one you declared? If not then it needs to be NSXMLParser when you declare variables of this class
NSXMLParser is a class, so changing your variable names to it is a bad thing No offense intended, but you might want to consider taking a step back and studying C and objective-c, and the differences between variables,instances, types, and classes. I think it will make your life easier.
yezaev is offline   Reply With Quote
Old 06-24-2009, 09:48 PM   #8 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 5
Default

Thanks for the info. I was assuming the sample project I was starting with was constructed sensibly. Perhaps that's assuming too much?

Quote:
Originally Posted by yezaev View Post
you might want to consider taking a step back and studying C and objective-c, and the differences between variables,instances, types, and classes. I think it will make your life easier.
That's sound advice, and I'm doing that too, but at the same time, I'm attempting to deconstruct and modify sample projects. This is how I taught myself several other technical subjects and I'm hoping the method will serve me here too.
Miles is offline   Reply With Quote
Old 06-25-2009, 02:54 AM   #9 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 6
Default

Whoops, sorry for the typos in the example code. Note to self: read before post! :P
Seidr 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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 23,966
Threads: 38,751
Posts: 170,022
Top Poster: smasher (2,560)
Welcome to our newest member, cruisetom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:49 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0