Question

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

Was it helpful?

Solution

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
    }
}

OTHER TIPS

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) {
...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top