RapidXML,读取和保存值

发布于 2024-08-20 07:05:07 字数 1401 浏览 15 评论 0原文

我自己研究了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”。

参考值 莱恩

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏剧牡丹亭 2024-08-27 07:05:07

我认为这是一个 RapidXML 陷阱

尝试添加 parse_no_data_nodes< /code> 标记为 cfg.parse<0>(pcSourceConfig)

I think this is a RapidXML Gotcha

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

玩心态 2024-08-27 07:05:07

您绝对应该测试输出文件是否正确打开以及写入是否成功。最简单的是,您需要类似的内容:

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

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

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.

帥小哥 2024-08-27 07:05:07

使用以下方法向节点添加属性。该方法使用来自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);
    }
}

Use the following method to add an attribute to a node. The method uses the allocation of memory for strings from rapidxml. So rapidxml takes care of the strings as long as the document is alive. See http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1modifying_dom_tree for further information.

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文