Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Dan Berry

macrumors newbie
Original poster
Jun 13, 2008
2
0
Good afternoon everyone. I am trying to create an application for the iPhone that will use an XML file for it's data source. I have been attempting to convert the SeismicXML application from the dev center to use a locally stored file, but to no avail. I have it building and running fine, but no data is appearing. Is there anyone that can give me an example of parsing with a locally stored file? Thanks!
 

pluto456

macrumors newbie
Jun 23, 2008
6
0
I'm a n00b to iPhone programming and found it a real PITA to get an XML parser working. After piecing together things from various Google searches, here's what I got to work:


Code:
#import <Foundation/Foundation.h>
#import <Foundation/NSXMLParser.h>

- (void)parserDidStartDocument:(NSXMLParser *)parser{	
	NSLog(@"found file and started parsing");
}

- (void)parseXMLFileAtURL:(NSString *)URL //URL is the file path (i.e. /Applications/MyExample.app/MyFile.xml)
{	
    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL fileURLWithPath:URL];
	
    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    NSXMLParser *parser = [[ NSClassFromString(@"NSXMLParser") alloc] initWithContentsOfURL:xmlURL];
	
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [parser setDelegate:self];
	
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
	
    [parser parse];
	
    [parser release];
}
 
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{			
    NSLog(@"found this element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}


Some other key points:
- having a bad XML file will also cause your parser to not work. This means unclosed tags, or not having the xml declaration at the top, like: "<?xml version="1.0" encoding="UTF-8"?>"
- also, you need to include some other stuff when you compile. Otherwise it will compile just fine, but won't parse anything. This is my include list: "-lobjc -framework CoreFoundation -framework Foundation -framework UIKit -framework CoreGraphics -framework GraphicsServices -framework IOKit -framework WebKit -framework LayerKit -framework OfficeImport" IOKit through OfficeImport seem to be necessary. I tried to leave one out and it just doesn't work
- finally, I'm doing all of this with the toolchain, so things might be different with the genuine SDK, but this works for me.

Good luck!
 

forrestgrant

macrumors member
Jun 24, 2008
34
0
So I also got it to work, but slightly different. In your XMLReader.m Class, when instantiating the NSXML, use data instead of URL like so:

Code:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:DATA];

Likewise, you need a full local path to the file, but this did the trick for me. How about luck parsing different types of xml, for instance the Seismic App using xml formatted:

Code:
<xml>
  <data>
    <name>Forrest</name>
    <age>25</name>
    <username>forrestgrant</username>
  </data>
</xml>

But the file I want to parse is more like:

Code:
<xml>
  <data name="Forrest" age="25" username="forrestgrant" />
  <other value="6" />
</xml>

I am having a very hard time getting those values... Anyone have any similar problems?
 

pluto456

macrumors newbie
Jun 23, 2008
6
0
attribute values for elements are stored in the attributeDict variable that's passed to parserDidStartElement:
For example, to get the name from your first data element, you would do this:
Code:
NSString *myFirstName= [attributeDict valueForKey:@"name"];
 

Dan Berry

macrumors newbie
Original poster
Jun 13, 2008
2
0
Forest, you said using initWithData worked for you. Where did you store the xml file for the app, and can you tell me what the full path was in your code? Thank you all for your help so far.
 

Enuratique

macrumors 6502
Apr 28, 2008
276
0
Forest, you said using initWithData worked for you. Where did you store the xml file for the app, and can you tell me what the full path was in your code? Thank you all for your help so far.

There's a whole section on Resource Management for the iPhone, but to save you the trouble of finding it on Apple's site - here's what I do. First, with your project open in XCode, right click the Resources folder in the tree on the left, select Add Existing file, select the XML file you want to add and check the Copy to local project folder checkbox (if the XML file doesn't already live with your other code files). This should make it show up in your Resources list along with your xib and plist files. When you build the application, it will bundle it up for you automagically. Then to get its file path in code do this:

Code:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myFile.xml"];

HTH,

Enuratique
 

forrestgrant

macrumors member
Jun 24, 2008
34
0
Good call, I hadnt noticed that you could use a local path like that. I did things almost exactly the same except my path looked more like:

Code:
NSString *file= @"/Users/me/Documents/App/file.xml";

Thanks for the tip.
 

acanby

macrumors newbie
Aug 9, 2008
4
0
thank you so much for this!!!! i have been tearing my hair out all day trying to get this to work.
 

tadelv

macrumors newbie
Sep 20, 2008
12
0
Hello everyone!
This is my first post here on MacRumors Forums, so please be gentle:)

I have problem with the NSXMLParser class in my app for iPhone. I am trying to parse a file from an URL and it seems that the file does not have a valid xml starting tag: "<?xml version="1.0" encoding="UTF-8"?>" that is. So the parser aborts and crashes my app.

I have tried to open the file to a string with initWithContentsOfURL... and appending the start tag at the beginning, but the weird thing is, that when i try to open the file to a string, i get a weird message from the server instead of the xml file...

What are the possibilites of opening the xml with an NSData object and then appending the start tag to this object?

Thanks for helping me out here!:apple:
 

taronga

macrumors newbie
Sep 27, 2009
1
0
XML File import from Mail

Is it possible to have an iPhone app import an XML file received in an email?
 

drivefast

macrumors regular
Mar 13, 2008
128
0
if your app goes and fetches the email itself from the server, yes it is possible. not easy, but possible. but if you're asking about reading the emails that the iphone native email app received, no, you dont have access to those emails.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
It might be possible for the user to copy the text from mail.app and paste it into your app. Cumbersome but might work.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.