快速XML提供空的CDATA节点

4
我写了下面的代码以获取CDATA节点值,我得到了节点的名称,但这些值是空白的。
我将解析标志(parse Flags)更改为parse_full,但它也没有起作用。
如果我手动从XML中删除"<![CDATA["和"]]>",它会按预期给出该值,但在解析之前删除它不是一个选项。
代码:
#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>


你能添加一个最小化的 text.xml 文件以重现问题吗? - Roddy
1个回答

6
当您使用CDATA时,RapidXML将其解析为层次结构中外部元素“下方”的单独节点。
通过使用nodeFrame->first_node()->name(),您的代码正确获取了“title”,但是由于CDATA文本位于单独的元素中,您需要使用以下内容提取值: cout << "value: " <<nodeFrame->first_node()->first_node()->value();

我太菜了,虽然在文档中读到过,但现在清楚地认识到我没有理解。:P 非常感谢!^_^ - Roger Russel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接