I am working on an Android application that interacts with a bluetooth camera. For each clip stored on the camera, we save some fields about the clip (some of which the user can change) in the XML file.
This application is currently the only application that writes this xml data to the device, but in the future it is possible that a desktop application or iphone application can write data here. I do not want to make the assumption that another application also cannot have additional fields (especially if they had a newer version of the application that added new fields, this version did not yet support).
So what I want to prevent is a situation where we add new fields to this XML file in another application, and then the user switches to using the android application and wipes it from these other fields, because he does not know about them.
So, let's take a hypothetical example:
<data> <title>My Title</title> <date>12/24/2012</date> <category>Blah</category> </data>
When reading from the device, this will be translated into a Clip object, which looks like this: (simplified for brevity)
public class Clip { public String title, category; public Date date; }
Therefore, I use SAX to analyze data and store it in a clip. I just store the characters in a StringBuilder and write them down when I get to the final element for name, category and date.
I realized that when I write this data back to the device, if there are any other tags in the original document, they will not write, because I write only the fields that I know of.
This makes me think that maybe SAX is the wrong option, and maybe I should use the DOM or something else where I could more easily write down any other elements that originally existed.
As an alternative, I thought, maybe my Clip class contains an ArrayList of some general XML type (possibly DOM), and in startTag I check to see if this element is one of the predefined tags, and if so, until until I get to the end of this tag I keep the whole structure (but in what?). Then, after recording, I just looked at all the additional tags and wrote them into an XML file (along with the fields that I know of course)
Is this a common problem with a well-known solution?
- Update 5/22/12 -
I did not mention that in the actual xml root node (actually called the annotation) we use the version number that was set to 1. What I am going to do in the short term requires that the version number supported by my application is> = version number from xml data. If xml is a larger number, I will try to parse for reading, but will refuse any model save. I'm still interested in some kind of working example, but how to do it.
By the way, I was thinking of another solution, which should be fairly simple. I believe that I can use XPATH to find the nodes that I know of and to replace the contents for these nodes when updating data. However, I did some tests, and the overhead is absurd when parsing xml when it is parsed in memory. Just the parsing operation, without even making any searches, led to the fact that the performance was 20 times worse than SAX. Using xpath was 30-50 times slower overall for parsing, which was very bad considering that I am parsing them as a list. So my idea is to SAX parse the nodes in the clips, but store all the XML in a variable of the Clip class (remember, this xml is short, less than 2 KB). Then, when I move on to writing data, I could use XPATH to replace the nodes that I know about in the source XML.
However, they are still interested in any other solutions. I probably will not make a decision, although it does not contain some code examples.