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.