我试图分析在Objective-C一些XML,使用TouchXML库 。 该XML在根元素名称空间,所以我使用该方法在如CXMLNode对象上的TouchXML库:
- (NSArray *)nodesForXPath:(NSString *)xpath namespaceMappings:(NSDictionary *)inNamespaceMappings error:(NSError **)error;
我的代码使用这种方法来选择一串匹配XPath查询,然后我做一些更多的XPath查询阅读一些属性的每个节点的节点。 出于某种原因,在第二组的查询的结果pointer being freed was not allocated
错误-我不能工作在哪里,这是来自哪里。
OK,这里的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库中的方法不与命名空间打扰,它工作正常(虽然我不会有能够在编辑XML奢侈品现实世界的情景)。
我知道,长期的,复杂的问题,可能与一帮其他可能的原因,但我绝对孤立的问题到这些半打行。