开发者

to print local xml file through NSData?

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]
                   initWi开发者_高级运维thBytes:[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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜