Warning: I have no clue about how to work with XML.
I have a simple application whose only purpose is to persist ten properties about an object (a log, if you will). I wrote it rapidly and it writes the properties by appending them to a plain text file. Problem solved.
Now, I read that for simple applications like this one XML would be a good alternative. I don’t plan to change my script (being so simple, it works very well), but that left me thinking.
Say you want to create a log using XML.
How do you write an entry to it?
Do you read the whole XML file, append the entry, and rewrite the whole thing again? What I don’t understand is how to “append” to an XML file, considering that you cannot “append” to the middle of a plain text file without reading it, parsing it, and writing it again.
Please orient me.
Cheers.
XML isn’t really a good fit for anything you need to incrementally append to. A well-formed XML document has a single root element that encompasses (pretty much) the entire document. eg:
Appending one more child in this example would require that you insert the new child between
<yet-another-child/>and the closing</my-root>. While it is possible to do this without having to rewrite the entire file by seeking to the end of<yet-another-child/>and writing the new tail, actually finding that position is complicated enough that sticking with something that supports true appends, like a flat text file, is probably the wiser option.