2012-01-01 27 views
1

我該如何編寫它,以便將其保存在XML文件中?如何將此XML示例寫入XML文件?

<Stations> 
    <station id="1"> 
     <title>Lehavim Railway station</title> 
     <latitude>31.369834</latitude> 
     <longitude>34.798207</longitude> 
    </station> 
</Stations> 

我有這部分代碼,但我不知道如何安排它,這樣如上圖所示,將被保存。

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, 
                  YES); 

    NSString * filePath = [paths objectAtIndex:0]; 

    filePath = [filePath stringByAppendingPathComponent:@"favorite"]; 
    filePath = [filePath stringByAppendingPathExtension:@"xml"]; 

    NSDictionary *station = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"Lehavim Railway station", 
           @"31.369834", 
           @"34.798207", 
           , nil ]; 

    [station writeToFile:filePath atomically:YES]; 

我也不知道如何更改站點部分中的id

回答

1

在本地iOS文件系統上,XML文件看起來與NSDictionary或NSArray編寫的plist files相同,因此程序員可以通過寫出嵌套的字典或數組並且考慮它們而經常「作弊」(或進行快捷方式? XML文件。

除了這些文件不是XML。而且你不能通過標準NSDictionary或NSArray方法寫出XML風格的屬性(即你的例子中的id = "1"位)。

您需要決定創建XML對象的類並允許您寫出它們。

Here is a related question with a selection of answers & choices這可以幫助你找出正確的路要走。對於我自己的項目,我真的很喜歡libxml2