4

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>

Roddy
  • 63,052
  • 38
  • 156
  • 264
Roger Russel
  • 714
  • 7
  • 17

1 Answers1

6

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

Roddy
  • 63,052
  • 38
  • 156
  • 264
  • I'm such a nube, I've read about it in the documentation, but it is now clear that I had not understood. :P Thanks a Lot! ^_^ – Roger Russel Jan 09 '14 at 20:42