Search code examples
c++xmllanguage-agnostictinyxml

How to best save XML files


I have a bunch of classes that each read in their values from an XML file using TinyXML.

I've done this so everything is in memory, and my user is using the app and making changes. If the user presses Save, I need to iterate through my objects and call the Save() function which writes out the XML file. Should I rebuild the XML file programmatically from scratch and write it out? Or should I use the same TiXmlHandle that I used to read the file, modify that and then write it back out? That way, the original order is kept. If I recreate the XML, then the order of the original would be changed.

I want the user to be able to modify the XML by hand, so I think it would be wierd for them to change it manually, only to have it reshuffled when saving. Am I understanding my options correctly?

Never used TinyXML before, or XML for that matter.


Solution

  • I would dispose of the reading handles once you have read the XML input.

    To write them back out you would typically have a writeXML() method in the parent or main document type object an then have that call a writeXML() method in each object in your design.

    ps. It's not unusual to have different XML reading and writing libs.
    pps. if you are concerned about the order in an XMl document that you can't enforce from your object hierarchy - you have some fundemantal design wrong.