When reading XML, should I try to model Objects (in OO sense) or leave the content as XML entities?
I am trying to decide between two approaches when reading "strongly typed" (schema-based) XML content using Object Oriented languages:
At first, I would create a typed class hierarchy to represent every possible element type of that schema with its typed properties and all. Then, when I parse a document, I would recursively scan every node and create a proper class instance, reproducing all the nesting, attributes, children (as collections), etc. Then, I could manipulate this object tree as I want in my application, and when saving back I would have to call the object(s)'
toXml()
method, or somehow "convert" the object back to XML format.Using some off-the shelf XML library (any high-level language have one or more), I would parse the document and have its structure already in memory. That would mean a tree of Nodes. I would then manipulate them directly and could save everything back to file using the library methods. Also, if my application needs representation of the data, I could create proxy objects whose properties and methods are actually referring to the underlying Node structure.
The questions are: how is it usually done? Is there a "right" way to map between XML and OO and back? Is there a well-known way XML is supposed to be used by OO, or a way OO is supposed to use XML?