How to parse an XML file with NSXMLParser - objective-c

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

Related

How can i parse local xml file instead of Web xml in Xcode?

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.

How to Pass Query String to local html file from xcode ios app

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.

Parsing a local xml file in iOS sdk

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

Display PDF in UIWebView using loadData

I am trying to display a PDF I have stored locally in a UIWebView. This is how I currently attempt to do this:
if (![[NSFileManager defaultManager] fileExistsAtPath:self.url]) {
LOG_ERROR(#"Couldn't load local file. File at path: %# doesn't exist", self.url);
return;
}
nsurl=[NSURL fileURLWithPath:self.url];
NSData *data = [NSData dataWithContentsOfFile:self.url];
LOG_DEBUG(#"data length:%d",[data length]);
[self.webView loadData:data MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
I have also tried passing nil for textEncoding, as well as using UIWebView's loadRequest. The result is a UIWebView that displays a blank page. No errors occur in the UIWebView delegate method. The strange thing is that data has the correct length, in bytes, for the PDF I am trying to display, which means the file is being found and loaded correctly.
Does anyone have an idea as to what might be going wrong here or how I can better debug this problem?
Below you can find two different approaches to open a .pdf file on your UIWebView; one from a url and one as NSData:
if (self.pdfDataToLoad) //Loading the pdf as NSData
{
[self.webView loadData:self.pdfData
MIMEType:#"application/pdf"
textEncodingName:#"UTF-8"
baseURL:nil];
}
else //Open the pdf from a URL
{
NSURL *targetURL = [NSURL URLWithString:#"https://bitcoin.org/bitcoin.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
}
It turns out the problem was something to do with the file format of the PDFs. I edited them using Illustrator and re-saved them. I guess Mobile Safari doesn't like the way Illustrator formatted the files because each was blank when viewed using the simulator's browser (although I could open the PDFs in regular Safari just fine).
The solution was to open the PDFs using Preview and re-save them. After running each PDF through the Preview save routine I was able to get each PDF to display in a UIWebView without changing any of my code.
You don't need NSData to load local file, loadRequest should work directly with NSURL
[self.webView loadRequest:[NSURLRequest requestWithURL:nsurl]];
I can only suggest to make NSURL like that
nsurl = [NSURL URLWithString:[self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData is used to load the pdf files when they are stored locally in any of the directories.
To load the pdf which was in your application, use the following code .
NSString *path = [[NSBundle mainBundle] pathForResource:#"FileNameHere"
ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

How to get content from URL asynchronously?

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.