我正在尝试使用TouchXML library在objective-c 中解析一些XML 。XML 在根元素中有一个命名空间,因此我在 CXMLNode 对象上使用 TouchXML 库中的方法,例如:
- (NSArray *)nodesForXPath:(NSString *)xpath namespaceMappings:(NSDictionary *)inNamespaceMappings error:(NSError **)error;
我的代码使用这种方法来选择一组与 XPath 查询匹配的节点,然后为每个节点执行更多的 XPath 查询以读取一些属性。出于某种原因,第二组查询导致了一个pointer being freed was not allocated
错误——我不知道这是从哪里来的。
好的,这是 XML 的片段:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
<name>Place 1</name>
<description><![CDATA[6-20 Luck Street Eltham 3095]]></description>
<Point>
<coordinates>145.151138,-37.712663,0.000000</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Place 2</name>
<description><![CDATA[The Pines Shopping Centre, Reynolds Road Doncaster East 3109]]></description>
<Point>
<coordinates>145.168620,-37.762135,0.000000</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Place 3</name>
<description><![CDATA[25 Main Street Greensborough 3088]]></description>
<Point>
<coordinates>145.102788,-37.702511,0.000000</coordinates>
</Point>
</Placemark>
</Document>
</kml>
所以我把它读入一个 CMLXmlElement,然后我有这段代码来读出每个 <Placemark> 元素:
_locations = [NSMutableArray array];
NSDictionary *mappings = [NSDictionary dictionaryWithObjectsAndKeys:@"http://earth.google.com/kml/2.2", @"kmlns", nil];
NSError *error = nil;
for (CXMLNode *node in [element nodesForXPath@"//kmlns:kml/kmlns:Document/kmlns:Placemark" namespaceMappings:mappings error:&error])
{
[_locations addObject:[[ONEMapLocation alloc] initWithXmlElement:(CXMLElement *)node]];
}
此代码运行没有问题。但是,initWithXmlElement
每个位置对象都会像这样初始化自己:
NSDictionary *namespaceMappings = [NSDictionary dictionaryWithObjectsAndKeys:@"http://earth.google.com/kml/2.2", @"kmlns", nil];
NSError *error = nil;
_name = ((CXMLNode*)[[xmlElement nodesForXPath:@"./kmlns:name/text()" namespaceMappings:namespaceMappings error:&error] lastObject]).stringValue;
_description = ((CXMLNode*)[[xmlElement nodesForXPath:@"./description/text()" namespaceMappings:namespaceMappings error:&error] lastObject]).stringValue;
NSString *rawCoordinates = _description = ((CXMLNode*)[[xmlElement nodesForXPath:@"./kmlns:Point/kmlns:coordinates/text()" namespaceMappings:namespaceMappings error:&error] lastObject]).stringValue;
NSArray *coordinateArray = [rawCoordinates componentsSeparatedByString:@","];
_latitude = [[coordinateArray objectAtIndex:1] floatValue];
_longitude = [[coordinateArray objectAtIndex:0] floatValue];
当这段代码运行时,它成功地解析了 XML 文档,但大约一秒钟后,应用程序因错误而崩溃pointer being freed was not allocated
。如果我注释掉这些行并将_name
,_description
等设置为虚拟值,则一切正常。
我还尝试从 XML 中提取命名空间,并使用 TouchXML 库中的方法,这些方法不受命名空间的影响,并且工作正常(尽管我无法在现实世界的场景)。
我知道,很长,很复杂的问题,可能有很多其他可能的原因,但我肯定已经将问题隔离到这六行。