XML data binding explained

XML data binding refers to a means of representing information in an XML document as a business object in computer memory. This allows applications to access the data in the XML from the object, rather than using the DOM or SAX to retrieve the data from a direct representation of the XML itself.

It makes it possible to read and write XML data using a programming language class library (e.g. C++, C#, Java), specifically created for a given XML data format.[1] Whilst it is possible to manually write a computer program to achieve this, XML data binding tools generate the source code to perform these tasks.

Description

An XML data binder accomplishes this by automatically creating a mapping between elements of the XML schema of the document we wish to bind and members of a class to be represented in memory.

When this process is applied to convert an XML document to an object, it is called unmarshalling (also called deserialization). The reverse process, to serialize an object as XML, is called marshalling.

Approaches to data binding can be distinguished as follows:

Difficulties

Since XML is a document-oriented format and objects are (usually) not document-oriented, simple XML data binding mappings may ignore some of the structural information embedded in an XML document. Specifically, information such as comments, XML entity references, and sibling order may not be preserved in the object representation created by the binding application. However, this is not always the case; sufficiently powerful XML data binding tools are capable of preserving 100% of the information stored in an XML document.

Similarly, since objects residing in computer memory are not inherently sequentially stored, and may include links to other objects (including self-referential links), simple XML data binding mappings may not be capable of preserving all the information about an object when it is marshalled to XML. However, sufficiently powerful data binding tools perform graph structure analysis on objects residing in memory to marshall (cyclic) object graph structures in XML by utilizing standard XML reference attributes.

Alternatives

An alternative approach to automatic data binding relies instead on hand-crafted XPath expressions that extract data from XML. This approach has some benefits but also has some drawbacks. First, the approach only needs proximate knowledge (e.g., topology, tag names, etc.) of the XML tree structure, which developers can determine by looking at the XML data. Furthermore, XPath allows the application to bind the relevant data items and filter out everything else, avoiding the unnecessary processing that would be required to completely unmarshall the entire XML document. The drawback of this approach is the lack of automation in implementing the object model and XPath expressions. Instead, the application developers have to create these artifacts manually, which is time-consuming, potentially error-prone, and hampers application maintenance when XML schemas and XML content models are updated. Another drawback is the lack of XML schema verification, which XML data bindings typically apply automatically during unmarshalling. Schema validity is typically required in secure applications.

Data binding in general

One of XML data binding's strengths is the ability to deserialize objects across programs, languages, and platforms.[2] You can dump a time series of structured objects from a datalogger written in C (programming language) on an embedded processor, bring it across the network to process in Perl and finally visualize in Octave. The structure and the data remain consistent and coherent throughout the journey, and no custom formats or parsing is required. This is not unique to XML. YAML, for example, is emerging as a powerful data-binding alternative to XML. JSON (which can be regarded as a subset of YAML) is often suitable for lightweight or restricted applications.

XML data binding frameworks

NameProgramming LanguageLicenseFirst releaseLast stable releaseCode generation from XSDCustom mappingNote
Apache Commons BetwixtJava0.8Dormant. Serializes objects to XML without requiring an XML schema definition
Apache XMLBeansJava5.1.1,
CastorJava1.4.1, Earlier versions also supported Java-to-SQL persistence but this has since been forked into a separate project
CodeSynthesis XSDC++ and proprietary4.0.0, with SAX or tree-like mapping into C++ classes
gSOAPC and C++ and proprietary2.8.131, Supports XML schema, WSDL, and SOAP; XML schemas are not required to serialize C/C++ data to XML; custom mapping of XML schema types to C/C++ types via a type mapping file and from C/C++ types to compatible XML schema types by source code annotation
Java Architecture for XML Binding (JAXB)Java?
JiBXJava1.2.6, Maps classes to XML schemas via bytecode manipulation
Liquid XML Data BinderC++, C#, Java, Visual Basic.Net, Visual Basic 6 (COM)Freeware and proprietrySupports XML schema (XSD), DTD, XDR, WSDL. Serializes XML to JSON and JSON to XML.
Liquid XML ObjectsC# and Visual Basic .Net (Supports XSD 1.1)Freeware and proprietryDirect replacement for XSD.exe. Integrated within Microsoft Visual Studio. Supports XML schema (XSD 1.0 and XSD 1.1), DTD, WSDL. Serializes XML to JSON and JSON to XML.
SimpleJava2.7.1,
System.Xml.SerializationC#?Part of the .NET framework, contains XML data binding classes; includes xsd.exe tool to generate classes from XSD schema
xmlbeansxxC++0.9.1, C++ port of Apache XMLBeans
XStreamJava1.4.10, Also capable of serializing to JSON
ZeusJava?3.5 beta,

See also

External links

Notes and References

  1. https://www.liquid-technologies.com/xml-data-binder/what-is-xml-data-binding-part-1 "What is XML Data Binding - Tutorial"
  2. https://www.ibm.com/docs/en/odm/8.12.0?topic=binding-what-is-xml "What is XML binding"