tags:

views:

29

answers:

4

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.

A: 

It really depends on the language that you are using for writing your application, but most modern languages have a library, either in their base framework or as an external, called an XML parser.

It's a library used to read and write XML files. What is important to see is that XML is a very hierarchical data structure, so you can't directly go into the file as text file and write to it.

Gimly
+1  A: 

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:

<my-root>
  <a-child/>
  <another-child/>
  <yet-another-child/>
</my-root>

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.

Laurence Gonsalves
Thanks @Laurence. I take the message that "ML isn't really a good fit for anything you need to incrementally append to".
Arrieta
A: 

If I really had to do this, I'd structure the XML so that it took look entries as children of the root element, open the file, readbackwards until I was at the start of the closing tag for the root element, write the new elements, and then write the root element again.

Mostly though, I just wouldn't do it. XML is not a good format for log entries (and I'm speaking as a big fan of XML). I might find it useful to have each entry as an XML fragment, but even that is unlikely.

If you really need an XML log format, log in a more sensible format for logging, and then produce XML reports on those logs as needed. If you rotate your log files (e.g. writing to appLog20101001.log today and appLog20101002.log tomorrow), then you can optimise by persisting the XML files for logs that'll no longer be appended to.

Jon Hanna
A: 

Log4J provides XML log appender. So you could log your entire application with XML entries if you like. For parsing the content, I would use the JAXB built-in API from Java 6. You can derive all the classes for editing given that you have either the XDS or XML Schema of the XML output structure.

Nik