Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ RapidXML,读取和保存值_C++_Xml_Rapidxml - Fatal编程技术网

C++ RapidXML,读取和保存值

C++ RapidXML,读取和保存值,c++,xml,rapidxml,C++,Xml,Rapidxml,我通过rapidXML源代码进行了工作,并设法读取了一些值。现在我想更改它们并将它们保存到我的XML文件中: 正在分析文件并设置指针 void SettingsHandler::getConfigFile() { pcSourceConfig = parsing->readFileInChar(CONF); cfg.parse<0>(pcSourceConfig); } 更改值并保存到文件 void SettingsHandler::setDefinitio

我通过rapidXML源代码进行了工作,并设法读取了一些值。现在我想更改它们并将它们保存到我的XML文件中:

正在分析文件并设置指针

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

    cfg.parse<0>(pcSourceConfig);
}
更改值并保存到文件

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();
}
但是,无法保存新值。我的代码只是将原始文件保存在值为“60”的地方,它应该是“10”

Rgds
Layne

您肯定应该测试输出文件是否正确打开以及写入是否成功。最简单的情况是,您需要以下内容:

if ( ! ofFileToWrite << "<?xml version=\"1.0\"?>\n" 
       << sStream.str() << '\0' ) {
    throw "write failed";
}
如果(!ofFileToWrite我认为这是一个


尝试将
parse\u no\u data\u nodes
标志添加到
cfg.parse(pcSourceConfig)
使用以下方法向节点添加属性。该方法使用rapidxml为字符串分配的内存。因此,只要文档处于活动状态,rapidxml就会处理这些字符串。有关详细信息,请参阅

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);
    }
}
void setString属性(
xml_文档和文档,xml_节点*节点,
常量字符串和attributeName、常量字符串和attributeValue)
{
//为属性值分配分配给文档的内存
char*rapidAttributeValue=doc.allocate_字符串(attributeValue.c_str());
//在给定节点上搜索属性
xml_属性*attr=node->first_属性(attributeName.c_str());
如果(attr!=0){//属性已存在
//仅更改现有属性的值
属性->值(rapidAttributeValue);
}else{//属性不存在
//为属性名分配分配给文档的内存
char*rapidAttributeName=doc.allocate_字符串(attributeName.c_str());
//使用给定的名称和值创建新属性
attr=doc.allocate_属性(rapidAttributeName,rapidAttributeValue);
//将属性附加到节点
节点->附加属性(属性);
}
}

我的写作肯定有效,只是他没有写我的新值。无论如何,执行您建议的检查当然很重要。您是否知道rapidXML不是一个真正的一致性xml解析器,甚至不接近,它可以执行大约10%的格式检查?是的,我知道,但我只需要这些函数,所以它不应该成为一个问题+与其他解析器相比,rapidXML速度相当快。这个问题大约在1年前就解决了,所有重要的事情都已经提到了,但是谢谢。
if ( ! ofFileToWrite << "<?xml version=\"1.0\"?>\n" 
       << sStream.str() << '\0' ) {
    throw "write failed";
}
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);
    }
}