I need to parse a local xml file which is in my resource folder when a UIButton is pressed. I know how to parse an XML file from the web. Now how can i display the xml content which is in my resource folder.
How do I parse a local XML file?
I assume from your tags that you're using NSXMLParser to parse the XML. In that case, you can do this:
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"xml"];
NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
// do whatever you want with xmlParser
Related
Hii it's easy for me to parse a XML file from a web URL using NSXML Parser but i found little bit difficulty in parsing the local XML file.?
consider this,
my parser.m has a method for
-(void)parseRssFeed:(NSString *)url withDelegate:(id)aDelegate {
[self setDelegate:aDelegate];
responseData = [[NSMutableData data] retain];
NSURL *baseURL = [[NSURL URLWithString:url] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];}
and i used this method for calling a RSS Feed in one of my viewcontroller.m as
- (void)loadData {
if (items == nil) {
[activityIndicator startAnimating];
Parser *rssParser = [[Parser alloc] init];
[rssParser parseRssFeed:#"http://--some RssFeed url---" withDelegate:self];
[rssParser release];
} else {
[self.tableView reloadData];
}
}
and now instead of the some web Rss Feed Url i have to load my local XML file.
Please help me in this coding where i have to change and include NSBundlePath Resource for my local XML FIle.
Thanks in advance!!!
For a local file you don't need the NSURLConnection to get the data. The following will get a local XML file named "local.xml" into an NSData object:
NSString *path = [[NSBundle mainBundle] pathForResource:#"local" ofType:#"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
At this point you should be able to call the same parsing code you use once you have the data from the remote xml file.
So far I've kept app data on remote servers in the plist format. Downloading and parsing this format is relatively easy:
NSURL *url = [NSURL URLWithString:#"http://www.mysite.com/data.plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfURL:url];
How can I download a text file with an arbitrary format using Objective-C?
An example of arbitrary format is a file that's two sections of CSV data separated by a few blank lines. Of course I'll know the format ahead of time.
Use -[NSString initWithContentsOfURL:encoding:error:, like so
NSURL *url = [NSURL URLWithString:#"http://www.mysite.com/data.plist"];
NSError *error = nil;
NSString *text = [[NSString alloc] initWithContentsOfURL: url
encoding: NSUTF8StringEncoding
error: &error];
This will get you the file's contents which you can treat however you need to.
I have follwing code that load html file in to webview
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"htm"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
if (htmlData) {
[webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#"localhost/..."]];
}
but I want to pass data or value as query string to the calling html file so that I could receive them at html side using java script.
I have a file called sample.xml and I need to retrieve the contents in that file, using NSXMLParser. I tried parsing the XML file by way of a URL earlier, but now I have an XML file. Can somebody tell me how to parse when I have a file? I am Using Xcode4 and iOS SDK 4.3 Thanks in advance.
When you parsed your data with an URL you should have done something like that:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfURL:<#(NSURL *)#>]];
// Tell NSXMLParser that this class is its delegate
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
// Kick off file parsing
[parser parse];
//[parser setDelegate:nil];
[parser release];
To parse a file, you simply have to transform the first line and replace [NSData dataWithContentsOfURL:<#(NSURL *)#>] with [NSData dataWithContentsOfFile:<#(NSString *)#>] where the NSString is the path to your file.
PS: Increase your acceptance rate
Instead of the url you can use this
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"temp" ofType:#"xml"]];
NSURL *baseURL=[NSURL URLWithString:url];
You can check the following tutorials for reading and parsing XML files in Objective-C.
Reading from a XML file in iOS.
Objective C: Parsing an XML file
I'm trying here to create NSXMLParser from content of URL, it work perfectly well but is there way I can make URL content to be received asynchronously and later create NSXMLParser?
NSURL *url = [[NSURL alloc] initWithString: #"http://www.Xmlfile.com"];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[url release];
Use NSURLConnection to fetch the data asynchronously into an NSData, then use initWithData instead of initWithContentsOfURL.