2010-06-30 67 views
0

如何通过本地路径NSXMLParser如何通过本地路径的NSXMLParser

这样

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:storePath]; 

其中StorePath中是这样的本地路径:

/var/mobile/Applications/56EEB906-2B73-493C-9BE7-2732440CDB69/Documents/xml1.php 

和我的代码像这样:

//NSURL *urla = [[NSURL alloc] initWithString:storePath]; 
NSURL *urla = [[[NSURL alloc] init] fileURLWithPath:storePath]; 

//NSData *dataa = [NSData dataWithContentsOfURL:urla]; 

NSLog(@"urls ois %@",urla); 

帮助我,为什么我得到空值

回答

0

试试这个:

NSArray *paths = NSSearchPathForDirectoriesInDomains(
        NSDocumentDirectory, 
        NSUserDomainMask, YES 
       ); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSURL *urla = [NSURL fileURLWithPath:[documentsDirectory 
         stringByAppendingString:@"/xml1.php"]]; 

更多细节在iPhone Application Programming Guide: Getting Paths to Application Directories

更新:有一个常见的误解是什么stringByTrimmingCharactersInSet。它不会替换字符串中的所有空格,它只会删除前导和尾随空格。

如果你想摆脱空间,你需要使用像stringByReplacingCharactersInRange:withString:这样的东西来替代它们。

但是:

  • 我不知道你会替换得到一个有效的路径那些字符串。据我所知,在模拟器上,应用程序的Documents文件夹的路径中实际上有空格(意外地在我链接的文档中调出)。
  • 我不知道为什么空间会打扰你。该字符串是一个有效的路径(当然,只要你实际上创建了<app>/Documents/xml1.php文件);并根据NSXMLParser文档,initWithContentsOfUrl接受任何完全合格的URL与NSURL支持的方案,并支持file://计划。

如果有该NSXMLParser返回一个特定的错误,把它添加到您的问题(或要求另一个),这样我们就可以帮助你的实际问题。

+0

感谢,但如何消除这些空间? file://localhost/Users/applefan/Library/Application%20Support/iPhone%20Simulator/3.1.3/Applications/134C5D58-D930-4F0A-B2F7-D25A170D6F09/Documents/xml1。php – ram 2010-06-30 06:32:32

+0

); \t \t NSString * documentsDirectory = [paths objectAtIndex:0]; \t documentsDirectory = [documentsDirectory stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; \t \t NSLog(@“documentsDirectory is%@”,documentsDirectory); \t \t \t \t NSURL * urlc = [NSURL fileURLWithPath:[documentsDirectory \t \t \t \t \t \t \t \t \t \t \t stringByAppendingString:@ “/ xml1.php”]]; \t \t \t \t \t \t \t // \t urlc = [urlc stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSLog(@“urls is%@”,urlc); urlc仍然有这样 ILE空间plz帮助 – ram 2010-06-30 06:40:14

+0

://localhost/Users/applefan/Library/Application%20Support/iPhone%20Simulator/3.1.3/Applications/FD5A8AF3-0B07-4D56-8477-97F0DCEF15B5/ Documents/xml1.php – ram 2010-06-30 06:41:44

0

我的猜测是因为你直接读取PHP文件而不是通过PHP传递,所以输出不是XML。

1

嘿,这一切什么不公平。

我的建议是,很简单

 NSString *[email protected]"/Users/farshore/Desktop/prasanna/XMLParsing/Books-6.xml"; //this is ur path of xml file 


     NSURL *url = [[NSURL alloc] initFileURLWithPath:path];