to print local xml file through NSData? - iphone

i want to take some xml data from local path,and to parse,but when i use following code
NSLog returns(content) different texts which is differed from xml file, how can i get exact xml data to check ,it consists correct xml data or not? any help please? when i parse , it returns nothing..i have saved the file as .xml and copied to local resource folder?
NSString *xmlFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"samp.xml"];
NSString *xmlFileContents = [NSString stringWithContentsOfFile:xmlFilePath];
NSData *data = [NSData dataWithBytes:[xmlFileContents UTF8String] length:[xmlFileContents lengthOfBytesUsingEncoding: NSUTF8StringEncoding]];
NSString *content=[[NSString alloc]
initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];
NSLog(#"%#",content);

This is almost assuredly an encoding problem. Make sure your xml file is in UTF8 or convert it to UTF8 before you try to create the NSData object. Once that's done, the following code produces the same output as input.
NSString *open = [NSString stringWithContentsOfFile: [#"~/Desktop/note" stringByExpandingTildeInPath] encoding: NSUTF8StringEncoding error: NULL];
NSData *data = [NSData dataWithBytes: [open UTF8String] length: [open lengthOfBytesUsingEncoding: NSUTF8StringEncoding]];
NSString *save = [NSString stringWithUTF8String: [data bytes]];
[save writeToFile: [#"~/Desktop/note2" stringByExpandingTildeInPath] atomically: NO encoding: NSUTF8StringEncoding error: NULL];
You'll probably want to use one of the NSXML classes, unless you want to do all of the parsing yourself.

Related

The operation couldn’t be completed. (Cocoa error 260.)

I'm new on IOS development and i'm working on a pdf application and i need to store a PDF file on a NSData variable, I have the PDF path but i get this message error when i try to put this pdf on the NSData variable using dataWithContentsOfFile her is my simple code :
NSError *error;
NSString *PdfPath = [NSString stringWithFormat:(#"%#"),document.fileURL ];
NSString *newPath = [PdfPath stringByReplacingOccurrencesOfString:#"file://localhost" withString:#""];
NSLog(#"OriginalPdfPath => %#", newPath);
NSData *pdfData = [NSData dataWithContentsOfFile:newPath options:NSDataReadingUncached error:&error];
NB : the pdf path is in this format : /Users/bluesettle/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/BBEF320E-7E2A-49DA-9FCF-9CFB01CC0402/ContractApp.app/Pro.iOS.Table.Views.pdf
thanks for your help
Cocoa error 260 is a NSFileReadNoSuchFileError (as listed in FoundationErrors.h), meaning the file could not be found at the path you specified.
The problem is that your path still contains encoded spaces (%20), because you're basing it on the URL. You can simply do this:
NSData *pdfData = [NSData dataWithContentsOfFile:[document.fileURL path]];
Try to use NSBundle
NSString *newPath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"pdf"]
Edit:
Than you can use bundleWithPath method, here is an example:
NSString *documentsDir= [NSString stringWithFormat:#"%#/Documents", NSHomeDirectory()];
NSString *newPath= [[NSBundle bundleWithPath:documentsDir] bundlePath];

Can't convert nsurl into base 64

I am doing this way. but i am not getting where is wrong.
NSString *strUrl = [NSString stringWithFormat:#"https://play.google.com/stor/apps/details?id=com.ShiftSharerfree_new&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5TaGlmdFNoYXJlcmZyZWVfbmV3Il"];
NSString *originalString = [NSString stringWithFormat:#"%#", strUrl];
NSData *data = [NSData dataFromBase64String:originalString];
NSString *data = [base64 originalString];
NSLog(#"data:%#", data);//[data base64EncodedString]);
Please guide me in above.
Perhaps you need a base64 conversion library, as mentioned here:
Convert between UIImage and Base64 string
I had a similar error with code for converting images to base64, using [base64 encode...] calls.
the first thing i'd noted is that the NSData and the NSString have the same name. Could this be the error?

NSString encoding for accents displays crazy characters

I am downloading data (text) from a server.
I have tried with both:NSISOLatin1StringEncoding and NSASCIIStringEncoding
But I keep seeing things like: {"estado":"M\u00e9xico"}
Noting that it should read México and not M\u00e9xico (with an accent over the e).
Looking online I figured that \u00e9 is in fact é link.
But the NSString is not able to interpret this and instead prints weird things on my UILabels:
I would really really appreciate your help on this.
Alsso, if you are itnerested, you can download the data from here: http://www.miorden.com/demo/iphone/estadoJSON.php
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.miorden.com/demo/iphone/estadoJSON.php"]];
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"Downloaded: %#", string);
string = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.miorden.com/demo/iphone/estadoJSON.php"] encoding:NSISOLatin1StringEncoding error:nil];
NSLog(#"Downloaded: %#", string);
I have been literally trying for days and it is killing me!
Thank you so much!
That appears to be unicode, try NSUTF8StringEncoding.
The data is in JSON format, so you'll need to JSON decode it too.
For example using this: https://github.com/TouchCode/TouchJSON
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.miorden.com/demo/iphone/estadoJSON.php"]];
NSError *error;
NSArray *array = [[CJSONDeserializer deserializer] deserializeAsArray:data error:&error];
NSLog(#"Test: %#", [[array objectAtIndex:11] valueForKey:#"estado"]);
outputs
2011-08-11 09:35:45.742 enctest[63236:407] Test: México

convert .zip file into NSData

Hey, Is it correct to initialize an NSData with a zip file? I want to convert a zip file into NSData and construct another file with the data (in simple language 'copy it'). I have the code as:
NSURL *theFileUrl = [NSURL URLWithString: #"file://localhost/Users/xxx/Desktop/testZippedFile.zip"];
NSData *data = [NSData dataWithContentsOfURL: theFileUrl];
When I, NSLog(#"Data: %#", data) , i do get some output but when I try to initialize an NSString with this data, it doesn't work:
NSString *str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
`NSLog(#"String: %#", string)`
I get the log as: String: PK
Can anyone point out my mistakes please.
Thanks in advance!
Why do it that way? NSFileManager will do it for you :)
[[NSFileManager defaultManager] copyItemAtPath:oldPath toPath:newPath error:nil];
However, this only works for files that are local - if you want to copy a file from a server, you should have a look at NSURLConnection to load the data and then NSData's writeToFile:atomically: method to save the contents to the file system (found here.)
PK is the output you should expect.
The first 2 Characters in every zip-file are PK. Then there are some unprintable chars and at some point after those there is a character with a value of 0
If you create an NSString out of NSData all values up to the first 0-value are taken into account.
NEVER convert binary data into NSString.

How to get the contents of a text file (stored locally in the documents directory) into an NSString?

How to retrieve the contents of a TEXT FILE stored locally in the documents directory?
Assuming the text is UTF-8 encoded:
NSData *textFileData = [NSData dataWithContentsOfFile:pathToTextFile];
NSString *textFileString = [NSString stringWithUTF8String:[textFileData bytes]];
You can also use this NSString class method (assuming the text is UTF-8):
NSString *content = [NSString stringWithContentsOfFile:pathToTextFile encoding:NSUTF8StringEncoding error:&error];
error should be a pointer to an NSError object.