RapidXML, reading and saving values

I worked through fastXML sources and was able to read some values. Now I want to change them and save to my XML file:

Parsing a file and setting 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 */
}

Change values ​​and save 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 to clipboard

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 is not possible to maintain the new value. My code just saves the source file with a value of "60", where it should be "10".

Rgds layne

+3
source share
3 answers

I think this is RapidXML Gotcha

Try adding a flag parse_no_data_nodestocfg.parse<0>(pcSourceConfig)

+2
source

, . - :

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

, '\ 0', .

+1

Use the following method to add an attribute to node. The method uses memory allocation for strings from quickxml. Therefore quickxml takes care of the lines while the document is alive. See http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1modifying_dom_tree for details .

void setStringAttribute(
        xml_document<>& doc, xml_node<>* node,
        const string& attributeName, const string& attributeValue)
{
    // allocate memory assigned to document for attribute value
    char* rapidAttributeValue = doc.allocate_string(attributeValue.c_str());
    // search for the attribute at the given node
    xml_attribute<>* attr = node->first_attribute(attributeName.c_str());
    if (attr != 0) { // attribute already exists
        // only change value of existing attribute
        attr->value(rapidAttributeValue);
    } else { // attribute does not exist
        // allocate memory assigned to document for attribute name
        char* rapidAttributeName = doc.allocate_string(attributeName.c_str());
        // create new a new attribute with the given name and value
        attr = doc.allocate_attribute(rapidAttributeName, rapidAttributeValue);
        // append attribute to node
        node->append_attribute(attr);
    }
}
+1
source

Source: https://habr.com/ru/post/1731661/


All Articles