RapidXML提供空的CDATA节点 [英] RapidXML giving empty CDATA nodes

查看:202
本文介绍了RapidXML提供空的CDATA节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也写了下面的代码来获取CDATA节点值,得到了节点的名称,但是这些值都为空.

I wrote the code bellow to get CDATA node value too, I got the node's name, but the values are in blank.

我将解析标志更改为parse_full,但它也无法正常工作.

I changed the parse Flags to parse_full, but it not worked too.

如果我从XML中手动删除<![CDATA ["和]]>",它会提供预期的值,但是在解析之前将其删除是不可能的.

If I manually remove "<![CDATA[" and "]]>" from the XML, It gives the value as expected, but removing it before parse is not a option.

代码:

#include <iostream>
#include <vector>
#include <sstream>
#include "rapidxml/rapidxml_utils.hpp"

using std::vector;
using std::stringstream;
using std::cout;
using std::endl;

int main(int argc, char* argv[]) {

    rapidxml::file<> xmlFile("test.xml");
    rapidxml::xml_document<> doc;
    doc.parse<rapidxml::parse_full>(xmlFile.data());

    rapidxml::xml_node<>* nodeFrame = doc.first_node()->first_node()->first_node();

    cout << "BEGIN\n\n";

    do {

        cout << "name:  " << nodeFrame->first_node()->name() << "\n";
        cout << "value: " << nodeFrame->first_node()->value() << "\n\n";


    } while( nodeFrame = nodeFrame->next_sibling() );

    cout << "END\n\n";

    return 0;
}

XML:

<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0" xmlns:c="http://base.google.com/cns/1.0">
<itens>
   <item>
    <title><![CDATA[Title 1]]></title>  
    <g:id>34022</g:id>
    <g:price>2173.00</g:price>
    <g:sale_price>1070.00</g:sale_price>
   </item>
    <item>
        <title><![CDATA[Title 2]]></title>  
        <g:id>34021</g:id>
        <g:price>217.00</g:price>
        <g:sale_price>1070.00</g:sale_price>      
    </item>
</itens>
</rss>

推荐答案

使用CDATA时,RapidXML将其解析为层次结构中外部元素下方"的单独节点.

When you use CDATA, RapidXML parses that as a separate node 'below' the outer element in the hierarchy.

您的代码通过使用nodeFrame->first_node()->name()正确获取了标题",但是-由于CDATA文本位于单独的元素中,因此您需要使用它来提取值:

Your code correctly gets 'title' by using nodeFrame->first_node()->name(), but - because the CDATA text is in a separate element, you'd need to use this to extract the value:

cout << "value: " <<nodeFrame->first_node()->first_node()->value();

这篇关于RapidXML提供空的CDATA节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆