如何在使用xpath时获取CXMLElement

时间:2010-11-03 20:06:55

标签: objective-c touchxml

当xpath查询文档时,有没有办法让CXMLElement回来? XCMLNode返回的- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;项不包含属性函数。

有没有办法直接获取元素或将节点转换为元素?

由于

2 个答案:

答案 0 :(得分:4)

你可以施展它,但一定要先检查类型:

for (CXMLNode *node in nodeArray)
{
    if ([node kind] == CXMLElementKind)
    {
        CXMLElement *element = (CXMLElement *)node;

        // do stuff with element
    }
}

答案 1 :(得分:0)

AFAIR CXMLElement是CXMLNode的子类。如果您确定xpath将返回CXMLElements,那么只需将CXMLNode强制转换为CXMLElement。在其他情况下,您应检查节点类型,然后进行强制转换。

来自touchXML文档:

NSArray *nodes = NULL;
//  searching for piglet nodes
nodes = [doc nodesForXPath:@"//piglet" error:nil];

for (CXMLElement *node in nodes) {
...
}