我有一个包含所有州点的文件。我想解析它,以便我可以为每个状态创建一个覆盖。我认为它是 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/