我在使用TouchXML library来解析objective-c中的一些XML。该XML在根元素命名空间,所以我用像CXMLNode对象在TouchXML库的方法:使用带有名称空间映射的[CXMLNode nodesForXPath]的内存崩溃
- (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这一点,那么我有这个代码读出每个<标>元素:
_locations = [NSMutableArray array];
NSDictionary *mappings = [NSDictionary dictionaryWithObjectsAndKeys:@"http://earth.google.com/kml/2.2", @"kmlns", nil];
NSError *error = nil;
for (CXMLNode *node in [element [email protected]"//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)。
我知道,很长,很复杂的问题,可能还有其他一些可能的原因,但我确实已经将问题隔离到了这些六条线上。
我其实已经在处理与命名空间选择节点的TouchXML方法追查这一个错误 - 我有一个粗略的修复,我会很快发布。下面的评论提醒我,如果你想要的答案。 – 2013-02-25 11:19:27
我自己现在就发生了这个问题 - 你的修复工作? – Ertebolle 2013-03-12 07:40:25