Question

I am using pugixml for the first time and I am not able to correctly load a file into memory.

My XML test document:

<?xml version="1.0"?>
<node>
</node>

I use following code to load the document and get the child:

pugi::xml_document doc;
pugi::xml_parse_result result = doc.load("test.xml");
if (result)
{
    Log(L"XML [%s] parsed without errors", L"test.xml");

    pugi::xml_node node = doc.child(L"node");
    if (node)
        Log(L"it works");
    else
        Log(L"it does not work");

    doc.save_file(L"test.xml");
}
else
{
    Log(L"XML [%s] parsed with errors", L"test.xml");
    Log(L"XML Error description: %s", result.description());
    Log(L"XML Error offset: %d", result.offset);
}

Output:

XML [test.xml] parsed without errors

it does not work

XML file after execution:

<?xml version="1.0"?>

(When using doc.save_file function data are lost)

Compiler: MS Visual Studio 2010 Ultimate

Language: C++

Build: UNICODE/x64

Was it helpful?

Solution

Instead of opening the file your code is parsing the string "test.xml". Use doc.load_file("test.xml").

doc.load("test.xml") did not return any errors because pugixml does not reject root-level PCDATA (but does not parse it either). This is not an ideal decision, but it is not clear if a significantly better alternative exists - there are complications with parsing document fragments and parsing valid fragments that do not generate any nodes (i.e. "<!--comment-->" without pugi::parse_comments).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top