文件xml的读取

一开始我在AppDelegate里把在程序里创建的help.xml文件复制到沙箱中://help.xml文件复制到沙箱中
NSString *filename1 = @"help.xml";
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);
NSString *sandboxPath1 = [paths1 objectAtIndex:0];
NSString *mainBundlePath1 = [[NSBundle mainBundleresourcePath];
//Document目录文件路径
NSString *dst_path1 = [NSString stringWithFormat:@"%@/%@",sandboxPath1,filename1];
//源文件目录文件路径
NSString *src_path1 = [NSString stringWithFormat:@"%@/%@",mainBundlePath1,filename1];
NSFileManager *fm1 = [NSFileManager defaultManager];
//把源文件目录下的文件拷贝到Document目录文件路径
    [fm1 copyItemAtPath:src_path1 toPath:dst_path1 error:nil];


这以后我的Document目录下就有这个xml文件了!
程序要求在有网络的情况下,从服务器解析获取数据来解析;在没网络的情况下读本地的xml文件;


于是我就在程序中写了文件的读和写:
//文件的读
-(NSString *)readFile{
//    NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath];
//    NSString *path = [mainBundlePath stringByAppendingPathComponent:@"help.xml"];
//    NSData *reader = [NSData dataWithContentsOfFile:path];    
//    return [[NSString alloc] initWithData:reader encoding:NSUTF8StringEncoding];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES); 
NSString *documentsDirectory = [paths objectAtIndex:0];
    [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"help.xml"]; 
NSData *reader = [NSData dataWithContentsOfFile:path]; 
return [[NSString allocinitWithData:reader encoding:NSUTF8StringEncoding]; 

}

Document//文件的写
-(void)writeFile:(NSString *)file{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);
NSString *documentsDirectory = [paths objectAtIndex:0];
    [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
    [fileManager removeItemAtPath:@"help.xml" error:nil];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"help.xml"];    
NSMutableData *writer = [[NSMutableData allocinit];
    [writer appendData:[file dataUsingEncoding:NSUTF8StringEncoding]];
    [writer writeToFile:path atomically:YES];
}


在有网络的情况下,我在程序里调文件的写的方法把help.xml写到Documents目录下;
在没网络的情况下,我在程序里调文件的读的方法

NSString *text = [self readFile];

NSLog(@"%@",text);
也能打印出来,可当我解析的时候就解析不出来了?


不知道为什么? 另外,我每次进程序,在有网络的情况下,我把解析下来的数据写到Document目录下,这是不是代表,如果服务器数据变化,我这就更新了......?因为每次调这个写的方法?



对了,我补充一句,我要在没网络情况下,是去读Document目录下的文件吗?还是其他的......另外,求大神们指点下我的思路对吗?



解决了,原来是我在没有进行解码出来的文件直接复制到程序创建的文件中;
NSData* newData = [Encode GB2312toUTF8ForNSDataXML:data];
    
    text = [Encode NSData2NSStringForUTF8:newData]; 

只有经过这步,才再复制,别看他和没解码前长的一样,其实你在解析的时候是解不出来的;

我是把在有网络解析出来,解过码的xml文件,把内容复制到到程序的xml文件中。



追加一句,查看程序中的xml文件转码没有,直接看xml文件的头
<?xml version="1.0" encoding="UTF-8"?>,后面跟没跟encoding="UTF-8"?,如果跟了,就没解码,没跟就解了!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值