使用libxml2 xpath从2个xml标签(打开和结束)中提取数据

时间:2014-08-29 09:09:05

标签: c++ xml libxml2

这是XML中的标记

<content>The Avengers</content>

我想使用C++中的libxml2中的xpath从标签中取出“复仇者”

这是我的代码

xmlXPathObject * xpathObj = xmlXPathEvalExpression( (xmlChar*)"/content", xpathCtx );
if ( xpathObj == NULL ) throw "failed to evaluate xpath";
for(int i=0;i<=0; i++)
{
    xmlNode *node = NULL;
    if ( xpathObj->nodesetval && xpathObj->nodesetval->nodeTab )
    {
        node = xpathObj->nodesetval->nodeTab[i];
        std::cout << "Found the node we want" << std::endl;
    }
    else
    {
        throw "failed to find the expected node";
    }

    xmlAttr *attr = node->properties;
    while ( attr )
    {
        std::cout << "Attribute name: " << attr->name << "  value: " << attr->children->content << std::endl;
        attr = attr->next;
    }
    xmlSetProp( node, (xmlChar*)"age", (xmlChar*)"3" );
}

它的工作正常并且在内容标记处很容易实现,但没有在标记之间显示数据

RESULT

<content age="3">The Avengers</content>

但在控制台中它没有显示任何内容 找到了我们想要的节点 谢谢

0 个答案:

没有答案