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++ C++;RapidXML-编辑XML文件中的值_C++_Xml_Rapidxml - Fatal编程技术网

C++ C++;RapidXML-编辑XML文件中的值

C++ C++;RapidXML-编辑XML文件中的值,c++,xml,rapidxml,C++,Xml,Rapidxml,我最近开始使用RapidXML,解析值很好(我可以从元素内部获取数据),但我想编辑元素内部的值 出于本程序的目的,我想将此: <?xml version="1.0" encoding="UTF-8"?> <root> <data> This is data. </data> </root> 有什么想法吗 编辑: 结合接受的答案,parse()函数还需要具有rapidxml::parse_no_

我最近开始使用RapidXML,解析值很好(我可以从元素内部获取数据),但我想编辑元素内部的值

出于本程序的目的,我想将此:

<?xml version="1.0" encoding="UTF-8"?>
<root>
     <data>
          This is data.
     </data>
</root>
有什么想法吗


编辑:

结合接受的答案,
parse()
函数还需要具有
rapidxml::parse_no_data_节点
标志:

std::string input_xml = TileManager::getData(filename);
std::vector<char> xml_copy(input_xml.begin(), input_xml.end());
xml_copy.push_back('\0');

rapidxml::xml_document<> doc;

doc.parse<rapidxml::parse_no_data_nodes>(&xml_copy[0]); // Notice the flag here
rapidxml::xml_node<>* root_node = doc.first_node("root");

std::string s = "test";
const char * text = doc.allocate_string(s.c_str(), strlen(s.c_str()));

root_node->first_node("data")->value(text);

std::string data;
rapidxml::print(std::back_inserter(data), doc);

std::ofstream file;
file.open(filename.c_str());
file << data;
file.close();
std::string input\u xml=TileManager::getData(文件名);
std::vector xml_copy(input_xml.begin(),input_xml.end());
xml_copy.push_back('\0');
rapidxml::xml_文档文档;
doc.parse(&xml_copy[0]);//注意这里的旗帜
rapidxml::xml_节点*根节点=文档第一个_节点(“根”);
std::string s=“测试”;
const char*text=doc.allocate_string(s.c_str(),strlen(s.c_str());
根节点->第一个节点(“数据”)->值(文本);
std::字符串数据;
rapidxml::打印(标准::背面插入器(数据),文档);
流文件的std::of;
打开(filename.c_str());
文件看看这个。使用RapidXML,您基本上必须确保写入文档的任何字符串在文档的生命周期内保持不变。在您的代码中,您正在分配一个临时变量,该变量在此调用之后将不存在

root_node->first_node("data")->value(std::string("Edited!").c_str());
试一试


它应该对你有用。请看一下将生成的XML输出到字符串方面的情况,解析标志rapidxml::parse_no_data_nodes是不够的。缺少声明节点

<?xml version=“1.0”encoding=“UTF-8”>

在输出文件中

例如,您必须使用类似的标志:

 doc.parse < trapidxml::parse_full | rapidxml::parse_no_data_nodes >(&xml_copy[0]);
doc.parse(&xml_copy[0]);
然后它就开始工作了。
[CentOS7.2]

明白了。更新了问题。
loadFile()
应该是什么?
root_node->first_node("data")->value(std::string("Edited!").c_str());
std::string new_value = "Edited!";
root_node->first_node("data")->value(new_value.c_str());
 doc.parse < trapidxml::parse_full | rapidxml::parse_no_data_nodes >(&xml_copy[0]);