views:

95

answers:

2

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.

A: 

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.

Martin Beckett
A: 

This first part isn't a direct answer to your question, but I'm going on your sentence "I've never used XML before." Because of the prevalence of XML in computing these days, it's worth spending some time to understand how XML works. For a straight-up reference, I recommend "XML in a Nutshell" by Harold and Means. It explains XML, schemas (which define exactly what the incoming XML must look like,) XPath (how you navigate nodes to arrive at the data you want), XQuery, XSLT, namespaces, etc.

What is really going to help you are the concepts, and for that you might want a different book (sorry, I don't have one to recommend.) It's important to know why you should use XML the right way, and not just to think of it as a glorified .INI file. Why the DOM concept is prevalent. Whether or not you should use SAX.

Probably the most relevant things to learn are XML schemas (XSDs). While it sometimes seems like overkill, you should strongly consider validating your XML against its schema (which is a one-method-call to a full XML parser such as xerces.) Remember, this is 2009, your program is running on a computer with a lot of memory, and it can do most of that stuff really, really fast; so it's totally worth it to know your data is good. And once you understand schemas, I'd recommend learning how to version them. That's especially important when dealing with program storage, because you can then make your programs understand previous versions of saved files and use or upgrade them automatically.

To more directly answer your immediate question, use TinyXML (or whatever parser you choose) for writing. Don't reinvent the output wheel when you already have the tool in hand.

John Deters