2

I'm going to parse an xml file into my iOS app. Well, I could do it when I just use the url of xml file, but I want to be able to parse the file when it is in Xcode project. so I used the following code:

NSURL *xmlFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Prices" ofType:@"xml"]];

NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:xmlFile];

but it doesn't parse, I even used the following as well

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Prices.xml"];
NSData *data = [[NSData alloc]initWithContentsOfFile:path];
NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:data];

still no parsing, but if I parse it from url it works

NSURL *url = [[NSURL alloc] initWithString:@"http://myurl/Data.xml"];

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

I need to parse the file inside the project not using the url, but after searching a lot on similar questions, I'm still not able to do it.

2
  • Does xmlParser gets initialized in all the above process, if NO, problem in your XML, if YES, problem in your process. Commented Aug 20, 2013 at 8:12
  • try this it may help you....stackoverflow.com/questions/4705588/nsxmlparser-example Commented Aug 20, 2013 at 8:14

1 Answer 1

3

I think your xml file can't be loaded because it is not part of your app bundle, because it is not a member of your app target.

Break your NSURL construction logic down into multiple lines to figure out the exact problem:

NSString *path = [[NSBundle mainBundle]pathForResource:@"Prices" ofType:@"xml"];
NSAssert(path, @"File does not exist in bundle! Check Target Membership. Or check for exact(!) filename match");
NSURL *xmlFile = [NSURL fileURLWithPath:path];
NSAssert(xmlFile, @"Could not create url from path. This should not happen because your problem is most likely in the path ;-)");

This code will check that path and xmlFile are not nil. path will be nil if Prices.xml could not be found in your bundle.

If your app throws an exception on the first NSAssert: Select the xml file in Xcode, open the File Inspector pane and in "Target Membership" make your xml File part of your Apps target.

If it does not throw an exception on any of those asserts you have to define what "doesn't parse" means. (or enable asserts)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, I don't get what is the "path" here, but I created the xml file inside the project and I think it can parse the file, but it doesn't show the contents, I guess it cannot put them into the array that I defined. to be more clear what I'm talking about, after following code BOOL worked =[xmlParser parse]; if (worked) { NSLog(@"Amount %i",[ListArray count]); } else{ NSLog(@"boo"); } when I run the project, it will show: Amount 0
Well, I it works now, but I just played with xml file, I wrote it again. now I know there was something wrong with xml file, but I don't know what was the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.