sharvey
sharvey

Reputation: 8155

How to get CXMLElement when using xpath

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

Upvotes: 1

Views: 1621

Answers (2)

dwineman
dwineman

Reputation: 701

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

Upvotes: 4

Yuras
Yuras

Reputation: 13876

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) {
...
}

Upvotes: 0

Related Questions