Question

In this simple code after invoking second(), 1.xml has only one node "1". Why pugi replaces node and what should I do for correct modifying?

void first()
{
    pugi::xml_document document;
    pugi::xml_parse_result result = document.load_file("C:\\1.xml", parse_full);
    pugi::xml_node node = document.append_child("0");
    node.append_attribute("message") = "something";
    document.save_file("C:\\1.xml");
}
void second()
{
    pugi::xml_document document;
    pugi::xml_parse_result result = document.load_file("C:\\1.xml", parse_full);
    pugi::xml_node node = document.append_child("1");
    node.append_attribute("message") = "something else";
    document.save_file("C:\\1.xml");
}
void test()
{
    first();
    second();
}
Was it helpful?

Solution

You should check the result of load_file.

Here's what's going on here:

  1. XML tag names can't start with digits. This is defined by XML standard.
  2. pugixml performs this check while loading the document - so load_file() fails, producing an empty document
  3. pugixml does not perform this check while appending nodes or saving document, so it is possible to save an invalid document
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top