Pergunta

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

Foi útil?

Solução

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

Outras dicas

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) {
...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top