简体   繁体   English

RapidXML,读取和保存值

[英]RapidXML, reading and saving values

I've worked myself through the rapidXML sources and managed to read some values. 我已经通过rapidXML源自己工作并设法读取一些值。 Now I want to change them and save them to my XML file: 现在我想更改它们并将它们保存到我的XML文件中:

Parsing file and set a pointer 解析文件并设置指针

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

    cfg.parse<0>(pcSourceConfig);
}

Reading values from XML 从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". 我的代码只是将原始文件保存为值“60”,其中应为“10”。

Rgds Layne Rgds Layne

I think this is a RapidXML Gotcha 我认为这是一个RapidXML Gotcha

Try adding the parse_no_data_nodes flag to cfg.parse<0>(pcSourceConfig) 尝试将parse_no_data_nodes标志添加到cfg.parse<0>(pcSourceConfig)

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. 请注意,您不需要'\\ 0'终止符,但它不应该造成任何伤害。

Use the following method to add an attribute to a node. 使用以下方法将属性添加到节点。 The method uses the allocation of memory for strings from rapidxml. 该方法使用来自rapidxml的字符串的内存分配。 So rapidxml takes care of the strings as long as the document is alive. 因此,只要文档处于活动状态,rapidxml就会处理字符串。 See http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1modifying_dom_tree for further information. 有关详细信息,请参阅http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1modifying_dom_tree

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM