2010-11-03 66 views
1

有沒有辦法在xpath查詢文檔時返回CXMLElement?由- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;返回的XCMLNode項目不包含屬性功能。如何在使用xpath時獲取CXMLElement

有沒有辦法直接獲取元素或將節點轉換爲元素?

由於

回答

4

你可以丟,但一定要檢查類型第一:

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

     // do stuff with element 
    } 
} 
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) { 
... 
} 
+0

其實我看了一個來源,並CXMLNode是在函數創建本身,所以它不是一個CXMLElement ... – sharvey 2010-11-03 20:22:27

+0

https://github.com/schwa/TouchXML/blob/master/Documentation/TouchXMLParsing.markdown – Yuras 2010-11-03 20:23:31