【问题标题】:RapidXML, reading and saving valuesRapidXML,读取和保存值
【发布时间】:2010-02-06 09:04:27
【问题描述】:

我自己研究了 rapidXML 源并设法读取了一些值。现在我想更改它们并将它们保存到我的 XML 文件中:

解析文件并设置指针

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

    cfg.parse<0>(pcSourceConfig);
}

从 XML 读取值

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

更改值并保存到文件

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

将文件读入缓冲区

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

但是,无法保存新值。我的代码只是将原始文件的值保存为“60”,而它应该是“10”。

Rgds 莱恩

【问题讨论】:

  • 您是否知道 rapidXML 并不是一个真正的符合性 xml 解析器,甚至不是很接近,它只进行了 10% 的良好格式检查?
  • 是的,我知道,但我只需要这些函数,所以这应该不是问题 + 与其他解析器相比,rapidXML 相当快。

标签: c++ xml rapidxml


【解决方案1】:

我认为这是RapidXML Gotcha

尝试将parse_no_data_nodes 标志添加到cfg.parse&lt;0&gt;(pcSourceConfig)

【讨论】:

    【解决方案2】:

    您绝对应该测试输出文件是否正确打开并且您的写入是否成功。最简单的情况是,您需要这样的东西:

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

    请注意,您不需要 '\0' 终止符,但它不应该造成任何伤害。

    【讨论】:

    • 我的写作确实有效,只是他没有写我的新价值观。无论如何,做你推荐的检查当然很重要。
    【解决方案3】:

    使用以下方法为节点添加属性。该方法使用来自 rapidxml 的字符串分配内存。因此,只要文档处于活动状态,rapidxml 就会处理字符串。请参阅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);
        }
    }
    

    【讨论】:

    • 这个问题大约在 1 年前得到解决,所有重要的事情都已经提到了,但是谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多