Saturday, August 20, 2011

Programming for XML: The Easy Way

I really like XML. It is really amazing what you can do with it. All you have to do is define a structure and then create a document that follows that structure. You can mix and match attributes and elements in individual tags or in lists of tags. The real power of XML is not in the static documents, but in having it be interpreted in running applications.

While I use a lot of programming languages on a regular basis, one of my favorites is C#. One of the reasons C# is so easy to use is because of the built in ability to serialize and deserialize objects directly to XML.  This ability is made possible by the XmlSerializer class. With simple annotations you can directly map the objects into the XML structure. The power of this approach is that you can build individual objects that represent each part of the XML and then load the entire document into memory with a single operation.


Java does not provide this functionality out of the box, but Simple XML provides a very small library that allows for an almost identical approach to be used in Java.


With the appropriate calls, the above code could easily read in and produce the following XML document:


While there are some differences between the C# approach and the Simple XML implementation, they are minor. Other than the minor syntax differences, they two handle arrays of objects slightly different. The biggest difference is the default behavior related to unexpected XML attributes and elements. C# is very forgiving and will be able to handle missing and extra elements and attributes without throwing an exception. However, it will throw an exception if there is a type mismatch such as trying to load a string into an integer. The Simple XML framework uses a strict parsing by default and the application will throw an exception if unexpected elements or attributes are encountered. Luckily there is a parameter that can override this behavior. The main reason I have encountered this requirement is to maintain backward compatibility as an XML schema changes over time.

My real complaint is the way the iPhone (Objective-C) deals with XML parsing. While there is no lack of parsing libraries on the iPhone, none of them use the object annotation approaches that I described above. This boils down to the different approach Objective-C uses with regard to syntax and the way objects are defined.

One of the projects I am currently working on requires that I port over several application libraries that has already been written in both C# and Java to Objective-C so it can run on the iPhone. While I have managed to avoid writing complex XML parsers up till now, it seems unavoidable at this point.

With respect to the iPhone, there is a built in plist parsers. Alternatively, JSON provides another means for document communication that would be somewhat easier. In the end I will have to live with my previous decision related to application architecture and choose an XML parsing engine and write the necessary code.

No comments: