简体   繁体   English

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

[英]C++ RapidXML - Edit values in the XML file

I recently started using RapidXML, and parsing the values is fine (I can get the data from inside the elements), but I want to edit the values inside of the elements. 我最近开始使用RapidXML,并且解析值很好(我可以从元素内部获取数据),但我想编辑元素内部的值。

For the purposes of this program, I want to turn this: 出于本程序的目的,我想转此:

<?xml version="1.0" encoding="UTF-8"?>
<root>
     <data>
          This is data.
     </data>
</root>

Into this: 进入:

<?xml version="1.0" encoding="UTF-8"?>
<root>
     <data>
          Edited!
     </data>
</root>

I read somewhere that the rapidxml::xml_node has a value() function to change the value inside of an element, but it doesn't seem to be working. 我在某处读到了rapidxml::xml_node有一个value()函数来改变元素内部的值,但它似乎没有用。 When I write out to the file, I get the exact same thing that I had before. 当我写出文件时,我得到了与以前完全相同的东西。 Here is my code: 这是我的代码:

std::string input_xml = loadFile(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_declaration_node | rapidxml::parse_non_destructive>(&xml_copy[0]);
// Also tried with doc.parse<0>(&xml_copy[0]) but no luck

rapidxml::xml_node<>* root_node = doc.first_node("root");

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

std::string data = std::string(xml_copy.begin(), xml_copy.end());

std::ofstream file;
file.open(filename.c_str());
file << data;
file.close();

Any ideas? 有任何想法吗?


Edit: 编辑:

In conjunction with the accepted answer, the parse() function needs to also have the rapidxml::parse_no_data_nodes flag: 结合已接受的答案, parse()函数还需要具有rapidxml::parse_no_data_nodes标志:

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

Then it will work. 然后它会工作。

Take a look at this http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1lifetime_of_source_text . 看看这个http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1lifetime_of_source_text With RapidXML, you basically have to ensure that any strings you write to the document persist for the lifetime of the document. 使用RapidXML,您基本上必须确保写入文档的任何字符串在文档的生命周期内保持不变。 In your code, you are assigning a temporary which will not exist after this call 在您的代码中,您将分配一个在此调用后不存在的临时值

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

try 尝试

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

and it should work for you. 它应该适合你。 Take a look at this also with regards to outputting the resulting XML to a string http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1printing 关于将结果XML输出到字符串http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1printing,请看一下这个问题。

parsing flag rapidxml::parse_no_data_nodes is not enough. 解析标志rapidxml :: parse_no_data_nodes是不够的。 There is missing declaration node 缺少声明节点

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

in the output file. 在输出文件中。

You have to use for instance similar flags : 你必须使用类似的标志:

 doc.parse < trapidxml::parse_full | rapidxml::parse_no_data_nodes >(&xml_copy[0]);

Then it's working. 然后它正在工作。 [CentOS7.2] [CentOS7.2]

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

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