문제

Is there a way to get a CXMLElement back when xpath querying a document? The XCMLNode item returned by the - (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error; doesn't contain the attributes functions.

Is there a way to either directly get an element or convert the node to an element?

Thanks

도움이 되었습니까?

해결책

You can cast it, but be sure to check the type first:

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

        // do stuff with element
    }
}

다른 팁

AFAIR CXMLElement is a subclass of the CXMLNode. If you are sure that the xpath will return CXMLElements, then just cast CXMLNode to CXMLElement. In other case you should check node type and then cast.

From touchXML documention:

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

for (CXMLElement *node in nodes) {
...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top