【问题标题】:How can I write this XML example to an XML file?如何将此 XML 示例写入 XML 文件?
【发布时间】:2012-01-01 18:05:04
【问题描述】:

我如何编写它以便将其保存在 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

【问题讨论】:

    标签: iphone objective-c xml


    【解决方案1】:

    在本地 iOS 文件系统上,XML 文件看起来与 NSDictionary 或 NSArray 写出的 plist files 相同,因此程序员可以经常“作弊”(或走捷径?)写出嵌套字典或数组并考虑它们是 XML 文件。

    除了这些文件不是 XML。而且您不能通过标准的 NSDictionary 或 NSArray writeToFile: 方法写出 XML 样式的属性(即您的示例中的 id = "1" 位)。

    您需要确定一个创建 XML 对象并允许您写出它们的类。

    Here is a related question with a selection of answers & choices 可以帮助您找出正确的方法。对于我自己的项目,我真的很喜欢libxml2

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      • 2018-07-26
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多