 |
 |
|
 |
06-24-2009, 07:09 AM
|
#1 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 5
|
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!
|
|
|
06-24-2009, 09:01 AM
|
#2 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 6
|
Quote:
Originally Posted by Miles
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.
|
|
|
06-24-2009, 01:47 PM
|
#3 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 5
|
Quote:
Originally Posted by Seidr
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!
|
|
|
06-24-2009, 02:00 PM
|
#4 (permalink)
|
|
Magic Hands' Daddy
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 22
Posts: 1,371
|
Quote:
Originally Posted by Miles
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
|
|
|
06-24-2009, 02:14 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 500
|
Quote:
Originally Posted by Miles
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.
|
|
|
06-24-2009, 02:22 PM
|
#6 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 5
|
Quote:
Originally Posted by smithdale87
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.
|
|
|
06-24-2009, 02:48 PM
|
#7 (permalink)
|
|
Registered Member
Join Date: Oct 2008
Posts: 92
|
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.
|
|
|
06-24-2009, 09:48 PM
|
#8 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 5
|
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
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.
|
|
|
06-25-2009, 02:54 AM
|
#9 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 6
|
Whoops, sorry for the typos in the example code. Note to self: read before post! :P
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 466 |
| 43 members and 423 guests |
| bbc z, beginer2007, Bitzal, cordoprod, DanielNovy, DaveHolzer, davidloew, dre, ebohdas, FlukeDude, harrytheshark, hermonir, hobbyCoder, howcr, isley, jaywright00, jbullfrog, JonnyBGoode, Juliaan, lukeca, melmoup, MiniRobinho, mJusticz, Mopedhead, mrarick, mriphoneman, nDev1, paul84, psilocybin, rahulskh, ronm3xico, ryguy2503, scleland, Straathond, swissmade, Tambourin, tidyguy, tpuser, tsktsk, upperhouse, vikinara, xslim, zackattack |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 23,966
Threads: 38,751
Posts: 170,022
Top Poster: smasher (2,560)
|
| Welcome to our newest member, cruisetom |
|