Frage

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();
}
War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top