Wednesday, February 6, 2013

XML Data Binding in Delphi 2007 (1)

Problem overview

  1. Wizards. There is a wizard named “XML Data Binding” in Delphi to help you generate a new unit which contains classes and interfaces mapped to a specified XML schema.
    image
    (F1: XML Data Binding in Delphi 2007)
    image
    (F2: XML Data Binding in Delphi XE)
    As the same wizard in Delphi XE is much advanced than the same one in Delphi 2007, I strongly suggest that you should use XE’s to generated the unit and copy it to your Delphi 2007’s project. Basically the unit generated by Delphi 2007 is not useful when the schema is complex (i.e it contains multiple name spaces and multiple imports etc.) The wizard in afterwards is always the one in Delphi XE.
  2. Name spaces. When the schema contains multiple name spaces, although the wizard can still generated the correct structure of the schema, the elements are always in the target name space regardless which name space they are actual in, according to the schema. This is due to the generated code does not use any name space to register a child node at all. This is not DOM’s problem, the MSXMLDOM has the capability to represent multiple name spaces. It is the implementation of the wizard that makes assumption that the XML has all elements in only one name space which is the target name space.
  3. Conformance to the schema. XML Data Binding is a mapping between the XML schema and the Delphi class. They describe the same thing in different format. These two formats have  overlap which covers most of the thing they are describing. But there are some areas that a Delphi class cannot easily achieve as same as the XML schema can. One of these area is the “sequence” restriction to element. XML Data Binding uses properties to represent the child element. Before reading from or writing to the property, the corresponding element has not been created into the DOM yet, it creates it immediately once a reading or writing operation is occurred. In another word, the order of the element is created by the order of the properties reading and writing. And this reading or writing order is totally decided by the programmer according to his/her taste. When it is time to output the final XML document, the order of the element will be the same of their creation order. This order might or might not conform the order specified by the XML schema it is mapping.

I have provided the solution for the first problem, in the next blog, I will provide solutions for the other two problems.