RapidXML, reading and saving values

user238801 picture user238801 · Feb 6, 2010 · Viewed 7.5k times · Source

I've worked myself through the rapidXML sources and managed to read some values. Now I want to change them and save them to my XML file:

Parsing file and set a pointer

void SettingsHandler::getConfigFile() {
    pcSourceConfig = parsing->readFileInChar(CONF);

    cfg.parse<0>(pcSourceConfig);
}

Reading values from XML

void SettingsHandler::getDefinitions() {    
    SettingsHandler::getConfigFile();
    stGeneral = cfg.first_node("settings")->value();
    /* stGeneral = 60 */
}

Changing values and saving to file

void SettingsHandler::setDefinitions() {
    SettingsHandler::getConfigFile();

    stGeneral = "10";

    cfg.first_node("settings")->value(stGeneral.c_str());

    std::stringstream sStream;
    sStream << *cfg.first_node();

    std::ofstream ofFileToWrite;
    ofFileToWrite.open(CONF, std::ios::trunc);
    ofFileToWrite << "<?xml version=\"1.0\"?>\n" << sStream.str() << '\0';
    ofFileToWrite.close();
}

Reading file into buffer

char* Parser::readFileInChar(const char* p_pccFile) {
    char* cpBuffer;
    size_t sSize;

    std::ifstream ifFileToRead;
    ifFileToRead.open(p_pccFile, std::ios::binary);
    sSize = Parser::getFileLength(&ifFileToRead);
    cpBuffer = new char[sSize];
    ifFileToRead.read( cpBuffer, sSize);
    ifFileToRead.close();

    return cpBuffer;
}

However, it's not possible to save the new value. My code is just saving the original file with a value of "60" where it should be "10".

Rgds Layne

Answer

Roddy picture Roddy · Mar 11, 2010

I think this is a RapidXML Gotcha

Try adding the parse_no_data_nodes flag to cfg.parse<0>(pcSourceConfig)