tags:

views:

594

answers:

3

Hello, 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

+1  A: 

You should definitely be testing that the output file opened correctly and that your write succeeded. At the simplest, you need something like:

if ( ! ofFileToWrite << "<?xml version=\"1.0\"?>\n" 
       << sStream.str() << '\0' ) {
    throw "write failed";
}

Note that you don't need the '\0' terminator, but it shouldn't do any harm.

anon
My writing definitely works it's just that he isn't writing my new values. Anyway, doing the check you recommended is of course important.
Layne
A: 

I think this is a RapidXML Gotcha

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

Roddy