objective-c - 这是什么类型的 XML 文件?

标签 objective-c ios xml cocoa nsxmldocument

我有一个包含所有州点的文件。我想解析它,以便我可以为每个状态创建一个覆盖。我认为它是 xml,但不是具有 element_data 格式的传统 XML。

有人提到它是一个 plist xml。

解析此类数据的最佳方法是什么:

<states>
<state name ="Alaska" colour="#ff0000" >
  <point lat="70.0187" lng="-141.0205"/>
  <point lat="70.1292" lng="-141.7291"/>
  <point lat="70.4515" lng="-144.8163"/>
  <point lat="70.7471" lng="-148.4583"/>
  <point lat="70.7923" lng="-151.1609"/>
  <point lat="71.1470" lng="-152.6221"/>
  <point lat="71.1185" lng="-153.9954"/>
  <point lat="71.4307" lng="-154.8853"/>
  <point lat="71.5232" lng="-156.7529"/>
  <point lat="71.2796" lng="-157.9449"/>
  <point lat="71.2249" lng="-159.6313"/>
  <point lat="70.6363" lng="-161.8671"/>
  <point lat="70.0843" lng="-163.5809"/>
  <point lat="69.3028" lng="-165.2399"/>
  <point lat="69.1782" lng="-166.8768"/>
  <point lat="68.3344" lng="-168.0414"/>

最佳答案

代码:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"data" 
                                                     ofType:@"xml"];

NSString* xmlStr = [NSString stringWithContentsOfFile:filePath
                                             encoding:NSUTF8StringEncoding 
                                                error:nil];

NSXMLDocument* xml = [[NSXMLDocument alloc] initWithXMLString:xmlStr
                                                      options:NSXMLDocumentTidyXML 
                                                       error:nil];

NSMutableDictionary* states = [xml getChildrenInDictionary:@"states"];

getChildrenInDictionary 方法是在我为 NSXMLDocument 编写的类别中声明的。

NSXML文档类别:

- (NSDictionary*)getChildrenInDictionary:(NSString *)path
{
    NSMutableDictionary* result = [NSMutableDictionary dictionary];

    NSArray* nodes = [[[self nodesForXPath:path error:nil] objectAtIndex:0] children];

    for (NSXMLElement* element in nodes)
    {
        [result setValue:[element stringValue] 
                  forKey:[element name]];
    }

    return result;
}
<小时/>

旁注:对我来说,这是一个完全有效的 XML 文件,因此应该对其进行处理。 :-)

关于objective-c - 这是什么类型的 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096184/

相关文章:

c# - 如何将录制的音频序列化为XML并反序列化以在C#/xaml Metro应用中播放?

java - 如何在我的 Android 应用程序上添加分享按钮

objective-c - NSThread 和更新 subview 中的对象

iphone - 将 UITabbarController 与 pushViewController 一起使用

ios - 订阅到期日期

java - 在启动画面 Android 上包含文本

ios - UIWebview内容不可见

ios - 双击单元格在 iOS9 上不起作用

iphone - iOS应用程序更新,如何更新包中的plist?

ios - NSString 哈希冲突不会弄乱 NSDictionary