2010-06-30 65 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];