RapidXML giving empty CDATA nodes

喜你入骨 提交于 2019-12-10 19:19:12

问题


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

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

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

The code:

#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;
}

The 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>


回答1:


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

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();



来源:https://stackoverflow.com/questions/21029167/rapidxml-giving-empty-cdata-nodes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!