Reputation: 191
I can read remote XML with the following code:
-(id)loadXMLByURL:(NSString *)urlString
{
tweets = [[NSMutableArray alloc] init];
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:urlString];
NSURL *url = [NSURL URLWithString:urlString];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
parser.delegate = self;
[parser parse];
return self;
}
And I'm calling the above function here:
- (void)viewDidLoad
{
xmlcont = [[XMLController alloc] loadXMLByURL:@"http://192.168.10.12:81/book.xml"];
for (Tweet *t in [xmlcont tweets]) {
NSLog(@"At date: %@ you wrote: %@",[t createdAt], [t content]);
}
[super viewDidLoad];
}
Actually I'd like to do something like this, little as one difference. I want to read a XML local file that is in my resource folder on my Project in XCode, but definitelly I don't know how to do this! I'm getting crazy with that! I'm just can to read XML remote file (http://..../file.xml).
Upvotes: 3
Views: 2655
Reputation: 44633
Say there is a file.xml
in your resources folder that you want to parse. You can get the file path like this,
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"xml"];
There are two ways you can use this to get what you want.
Using file URL
NSURL * fileURL = [NSURL fileURLWithPath:filePath];
NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];
....
Using file data
NSData * fileData = [NSData dataWithContentsOfFile:filePath];
NSXMLParser * parser = [[NSXMLParser alloc] initWithData:fileData];
....
Upvotes: 7