2011-11-29 27 views

回答

0

您可以使用下一個樣品爲您的任務

NSData *data = [inputString dataUsingEncoding:NSUTF8StringEncoding];  
xmlDocPtr doc = xmlReadMemory([data bytes], [data length], "", NULL, XML_PARSE_RECOVER); 

xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); 
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(BAD_CAST("//a"), xpathCtx); 

if (xpathObj && xpathObj->nodesetval) { 
    for (int i = 0; i < xpathObj->nodesetval->nodeNr; ++i) { 
     xmlNodePtr node = xpathObj->nodesetval->nodeTab[i]; 
     xmlChar *hrefProp = xmlGetProp(node, BAD_CAST("href"); 
     xmlChar *content = xmlNodeGetContent(node); 

     NSString *hrefString = [NSString stringWithUTF8String:(char *)hrefProp]; 
     NSString *contentString = [NSString stringWithUTF8String:(char *)content]; 
     xmlFree(hrefProp); 
     xmlFree(content); 

     NSLog(@"Anchor %@, Content: %@", hrefString, contentString); 
} 
xmlXPathFreeContext(xpathCtx); 
xmlXPathFreeObject(xpathObj); 
xmlFreeDoc(doc); 

有關設置的libxml2仔細閱讀部分Setting up your project file。 您也可以從網站下載工作示例並進行編譯。

+0

這是使用libxml2.2和目標c? –

+0

當然是的,這是iOS的真實項目的示例 – Victor

+0

另外,您還可以閱讀關於在objective-c中使用libxml2的精彩文章http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath .html – Victor

相關問題