Load XML from documents directory - iPhone - iphone

Instead of loading a XML from the URL, I would like to load it from the documents directory(which i saved when there is Internet connection)
NSString* filename=#"magzine.xml";
NSData *data=[NSData dataWithContentsOfURL:[[NSURL alloc] initWithString:[URLlink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSString *applicationDocumentsDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:filename];
[data writeToFile:storePath atomically:YES];
NSLog(#"URL: %#",url);
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//xmlParser = [[NSXMLParser alloc] initWithContentsOfFile:filename];
[xmlParser setDelegate:self];
[xmlParser parse];
Now i am getting the xml from url ,that is from initwithcontentsofurl,Now i want to get the saved file from documents directory when there is no internet connection.Please help me in this ,how to load the xml??
THANKS IN ADVANCE

Here is an example on how to load and parse local XML files on the iPhone.

Related

How to get path of file to save data after getting data from server?

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:strFileUrl]]; //strFileURL is url of my video/image
NSURLConnection *conection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
[conec start];
[request release];
with use of above code i am creating URL connection to download data from server.
strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:strFileName];
with use above code i can get path of file to save data, but in the above code i don't know how to get strFileName (at last in the above code) or please any one help me how to save data into my document directory after getting data from server. Advance thanking to all.
//Capturing server response
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[(NSURLResponse *)response suggestedFilename]];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:fullPath contents:result attributes:nil];

XML parsing text encoding problem

I need to parse a XML feed with french accent like 'é' .
When I parse the XML I lost the accents...
What's wrong in my code...
NSString *url = [NSString stringWithFormat:#"%#",#"my_xml_url"];
NSURL *myURL = [NSURL URLWithString:url];
NSString *myData = [[NSString alloc] initWithContentsOfURL:myURL];
XMLParser *parser = [[XMLParser alloc] init];
[parser parseXMLFile:myData];
And when I start parsing.....
- (void)parseXMLFile:(NSString *)data {
BOOL success;
//array for the ranking
rows = [[NSMutableArray alloc] init];
index = 0;
self.currentString = [NSMutableString string];
storingCharacters = NO;
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[data data UsingEncoding:NSUTF8StringEncoding]];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:YES];
success = [parser parse];
self.currentString = nil;
//release memory
[parser release];
}
I can't see where is the problem...
Thanks
It appears to be an encoding issue with stringWithContentsOfURL
Try:
NSError *lookupError = nil;
NSString *myData = [NSString stringWithContentsOfURL:myUrl
encoding:NSUTF8StringEncoding error:&lookupError];
You're first reading the XML data and converting it to an NSString, then later converting that string back into data. As krtrego said, the to-string conversion is probably picking the wrong encoding. Instead, you should feed the raw data from the URL directly into NSXMLParser, which is smarter about being able to interpret the correct encoding (i.e. using the metadata at the top of the XML file.)
So instead use
NSData* myData = [[NSData dataWithContentsOfURL: url options: 0 error: &error];
then pass that data when initializing the NSXMLParser.

Reading local XML file makes NSXMLParser return NULL

I have a local xml file which I know contains UTF-8 well-formed xml. But NSXMLParser will not parse correclty.
Path to file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"example.xml"];
Saving the xml from web:
NSURL *xml = [NSURL URLWithString:#"http://www.example.com/example.xml"];
NSString *data = [[NSString alloc] initWithContentsOfURL:xml encoding:NSUTF8StringEncoding error:nil];
[data writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
Parsing the xml:
NSURL *xmlFile = [NSURL URLWithString:appFile];
NSXMLParser *addressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlFile];
If I try parsing the NSURL "xml" directly it works, but the code above will return NULL and no errors. I found another post here on the forum describing the exact same thing, but I cannot find it again, and the post had no answers.
Hope someone can help! Basically it is just a matter of storing a web xml to file, and then feeding it to NSXMLParser.
Change this line:
NSURL *xmlFile = [NSURL URLWithString:appFile];
to this:
NSURL *xmlFile = [NSURL fileURLWithPath:appFile];
Because appFile is a local file system path and not an internet URL.

how to update data from web to exisitng file for iphone

NSString *applicationDocumentsDir =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:#"sample1.cfg"];
NSURL *instructionsURLd = [[NSURL alloc] initFileURLWithPath:filePath];
NSData *dataXML = [NSData dataWithContentsOfURL:instructionsURLd];
[dataXML writeToFile:storePath atomically:YES];
NSLog(#"matrix path %#",applicationDocumentsDir);
NSLog(#"neo path %#",storePath);
using this i can save data from my app to Documents folder right now sample1.cfg ia having 10 data now i want to add 10 more from web when user click on update button ?? so how to add 10 more data plz help
Thanks
...
NSData *dataXML = [NSData dataWithContentsOfURL:instructionsURLd];
NSMutableData *file = [[NSMutableData alloc] initWithContentsOfFile:storePath];
[file appendData:dataXML];
[file writeToFile:storePath atomically:YES];
...
This'll append the dataXML. If you want to prepend the data, use this:
...
NSMutableData *dataXML = [[NSData dataWithContentsOfURL:instructionsURLd] mutableCopy];
NSData *file = [[NSData alloc] initWithContentsOfFile:storePath];
[dataXML appendData:file];
[dataXML writeToFile:storePath atomically:YES];
...

How to create iPhone application can download content

Now I try to build iPhone application, like magazine online, that can download new magazine form web and store in app (can play offline).
I no idea for implement feature download new magazine form online source and store in the app.
Please help me.
Here's an example how you can download a file to your iPhone and reuse it later (in this case it's stores an HTML document that is then loaded to UIWebView from local store).
// downloading file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *dataPath = [paths objectAtIndex:0];
NSString *fileName = [[url path] lastPathComponent];
NSString *filePath = [dataPath stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO) {
NSString *fileString = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
[fileString writeToFile:filePath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
}
// use local file
NSURL *loadUrl = [[NSURL alloc] initFileURLWithPath: filePath];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: loadUrl];
[webView loadRequest: request];
[request release];
[loadUrl release];