RapidXML giving empty CDATA nodes

2019-07-06 08:51发布

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条回答
Deceive 欺骗
2楼-- · 2019-07-06 09:29

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

查看更多
登录 后发表回答