C++ RapidXML - Edit values in the XML file

2019-06-24 02:46发布

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.

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. 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:

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.

标签: c++ xml rapidxml
2条回答
可以哭但决不认输i
2楼-- · 2019-06-24 03:16

parsing flag rapidxml::parse_no_data_nodes is not enough. There is missing declaration node

< ?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]

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-06-24 03:24

Take a look at this 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. 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

查看更多
登录 后发表回答