0
我完全是XML,libxml和xpath的新手。因此,我想分析一个非常简单的XML表达libxml:找不到xpath?
<count>
<active type="integer">1</active>
</count>
我想检索值1.我写的libxml使用下面的代码(此代码包含目标C代码):
const unsigned char* xPathExp = (const unsigned char*) "count/active/text()";
xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xPathExp, xpathCtx);
xmlNodeSetPtr nodeSetPtr = xpathObj->nodesetval;
if(nodeSetPtr != 0) {
xmlNode* node = nodeSetPtr->nodeMax > 0 ? nodeSetPtr->nodeTab[0] : 0;
if(node != 0) {
NSLog(@"Active Projects = %@", [self stringFromCString:node->name]);
} else {
NSLog(@"Node for xpath Exp: count/active/text() does not exist.");
}
} else
NSLog(@"nodeSetPtr is null");
xmlFreeDoc(doc);
xmlCleanupParser();
它事实证明,这种代码的输出是
nodeSetPtr is null
所以这让我困惑。我在上面的xml上运行了一个在线xpath评估器,并且xpath表达式“count/active/text()”返回1.
我在做什么错误?