I have a 65Mb XML file that I am loading and modifying with PUGIXML. Problem is that I need to keep it updated and saved to disk so that other files can access. I don't want to implement a database, so is there a way for me to make modifications, and just save those few modifications, instead of dumping the entire document to file? I notice about a 3-4 second delay each time I save the xml file.
1 Answers
This is an old question. But anyway, let me answer.
pugixml has a mode of saving data via a write
interface. This effectively solves the problem of having to dump the entire file because you can chunk the data into smaller pieces and write to a file.
From the documentation:
This is a simple interface with a single function, which is called several times during output process with chunks of document data as input:
class xml_writer { public: virtual void write(const void* data, size_t size) = 0; }; void xml_document::save(xml_writer& writer, const char_t* indent = "\t", unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const;
...
write function is called with relatively large blocks (size is usually several kilobytes, except for the last block that may be small), so there is often no need for additional buffering in the implementation.
And there's one more way to solve this problem: by saving one subtree at a time. Again, from the documentation:
To save a single subtree, the following functions are provided:
void xml_node::print(std::ostream& os, const char_t* indent = "\t", unsigned int flags = format_default, xml_encoding encoding = encoding_auto, unsigned int depth = 0) const;
void xml_node::print(std::wostream& os, const char_t* indent = "\t", unsigned int flags = format_default, unsigned int depth = 0) const;
void xml_node::print(xml_writer& writer, const char_t* indent = "\t", unsigned int flags = format_default, xml_encoding encoding = encoding_auto, unsigned int depth = 0) const;
These functions have the same arguments with the same meaning as the corresponding xml_document::save functions, and allow you to save the subtree to either a C++ IOstream or to any object that implements xml_writer interface.

- 2,809
- 2
- 18
- 39