XML Data Binding for Java

Reasons to Use XML Data Binding

  • Reduces Development Time
  • Increased reliability (spot problems at compile time not runtime)
  • Creates more readable code
  • Fastest root between code and XML

Reasons To Use Liquid XML Data Binding Code Generator

  • Compiles on Linux.
  • Includes runtimes for gcc
    3.2, 3.2.2 & 3.4.1, 4.0 (64bit
    )
  • Runtime Source Code available which will compile on most compilers.
  • Thread Safe
  • Supports XSD, XDR & DTD's
  • Generates simple intuitive code
  • Supports the Fast Infoset XML compression
  • Generates Documentation with the code.
  • Royalty free distribution

Xml Data Binding

XML Data Binding allows you to treat your XML documents as objects within your application. This makes dealing with XML data from a programming language a simple matter of manipulating these strongly typed objects.

Liquid XML Data Binding Code Generator takes an XML Schema and uses it to generate a class library. These classes map directly to to data contained in your XML Schema, so if your schema has an Person entity and that has a date of birth attribute, then a class called Person will be generated, which will have a property called DateOfBirth.

Standard Support

The use of XML is exploding, and with it the number and complexity of 3rd party standards. There are now 1000's of XML Schemas describing everything from Solar orbits to coffee bean quality.

Because the XSD standard is complex, many XML Data Binding tools only support a sub set of it. This may be adequate for small in house schemas, but in the real world you need something that will cope with the standards that exist out in the wild. Liquid XML Data Binder has an unprecedented level of support for the W3C XSD Standard allowing it to cope with the most complex standards, just try it and see.

Tools Included with Liquid XML Studio

Liquid XML Data Binding Code Generator is included in the Developer Edition of Liquid XML Studio, which includes many other XML tools including
XSD Editor, XML Editor, XML Differencing tool, XPath Query viewer, Web Service Call Composer and more... see Liquid XML Studio.

A Simple Example

The following example we will use the Person.xsd, shown below to demonstrate how to read and write an XML document based on this schema.

XML Schema - Bookstore.xsd - Click to View

Sample XML File - BookStoreSample.xml - Click to View

Serializing the XML (Objects to XML) - Code to create an XML document using XML Data Binding Objects - Click to View

Using XML Data Binding

Using the standard DOM approach

 1Person p = new Person();
 2p.setName("Fred");
 3p.setDateOfBirth(new DateTime(1978, 6, 26));
 4
 5Address a = new Address();
 6p.SetAddress(a);
 7p.getAddress().setHouseNo(7);
 8p.getAddress().setPostCode("WV6 6JY");
 9
10Car runAroundCar = new Car();
11p.getCars().add(runAroundCar);
12runAroundCar.setModel("Ford");
13runAroundCar.setMake("Escort");
14
15Car toyCar = new Car();
16p.getCars().add(toyCar);
17toyCar.setModel("Lotus");
18toyCar.setMake("Elise");
19
20System.out.println(p.toXml());
 1DocumentBuilderFactory dbf =
 2 DocumentBuilderFactory.newInstance();
 3DocumentBuilder db = dbf.newDocumentBuilder();
 4Document xmlDoc = db.newDocument();
 5
 6Element rootElement = xmlDoc.createElement("Person");
 7xmlDoc.appendChild(rootElement);
 8
 9Element xmlElmName = xmlDoc.createElement("Name");
10xmlElmPerson.appendChild(xmlElmName);
11xmlElmName.setNodeValue("Fred");
12
13Element xmlElmDOB = xmlDoc.createElement("DateOfBirth");
14xmlElmPerson.appendChild(xmlElmDOB);
15xmlElmDOB.setNodeValue("1978-06-26");
16
17Element xmlElmAddress = xmlDoc.createElement("Address");
18xmlElmPerson.appendChild(xmlElmAddress);
19
20Element xmlElmHouseNo = xmlDoc.createElement("HouseNo");
21xmlElmAddress.appendChild(xmlElmHouseNo);
22xmlElmHouseNo.setNodeValue("7");
23
24Element xmlElmPostCode = xmlDoc.createElement("PostCode");
25xmlElmAddress.appendChild(xmlElmPostCode);
26xmlElmPostCode.setNodeValue("WV6 6JY");
27
28Element xmlElmCarRA = xmlDoc.createElement("Car");
29xmlElmPerson.appendChild(xmlElmCarRA);
30
31Element xmlElmRAModel = xmlDoc.createElement("Model");
32xmlElmCarRA.appendChild(xmlElmRAModel);
33xmlElmRAModel.setNodeValue("Ford");
34
35Element xmlElmRAMake = xmlDoc.createElement("Make");
36xmlElmCarRA.appendChild(xmlElmRAMake);
37xmlElmRAMake.setNodeValue("Escort");
38
39Element xmlElmCarToy = xmlDoc.createElement("Car");
40xmlElmPerson.appendChild(xmlElmCarToy);
41
42Element xmlElmToyModel = xmlDoc.createElement("Model");
43xmlElmCarToy.appendChild(xmlElmToyModel);
44xmlElmToyModel.setNodeValue("Lotus");
45
46Element xmlElmToyMake = xmlDoc.createElement("Make");
47xmlElmCarToy.appendChild(xmlElmToyMake);
48xmlElmToyMake.setNodeValue("Elise");
49
50//Serialize DOM
51OutputFormat format = new OutputFormat(doc);
52StringWriter stringOut = new StringWriter();
53XMLSerializer serial = new XMLSerializer (stringOut,
54 format);
55serial.serialize(doc);
56
57System.out.println(stringOut.toString());
The XML Data Binding code is much easier to read, more concise and will allow errors to be detected at compile time should the data model change.
The DOM code is verbose, difficult to read, and should the data model change, the errors will only be picked up in testing.

Deserializing the XML (XML to Objects) - Code to read an XML document using XML Data Binding Objects - Click to View

Using XML Data Binding

Using the standard DOM approach

 1Person person = new Person();
 2
 3// Load the XML
 4person.fromXmlFile("SampleFile.xml");
 5
 6System.out.println(
 7  person.getName() +
 8  " was born " +
 9  person.getDateOfBirth().toString() +
10  ", and lives at " +
11  person.getAddress().getHouseNo() +
12  ", " +
13  person.getAddress().getPostCode());
14
15System.out.println(
16  "Cars Owned " + person.getCars().count());
17
18for (int i = 0; i < person.getCars().count(); i++)
19{
20  Car car = person.getCars().getItem(i);
21  System.out.println(
22      " " + car.getMake() +
23      ", " + car.getModel());
24}
 1File file = new File("SampleFile.xml");
 2DocumentBuilderFactory dbf =
 3            DocumentBuilderFactory.newInstance();
 4DocumentBuilder db = dbf.newDocumentBuilder();
 5Document xmlDoc = db.parse(file);
 6
 7Element xmlElmPerson = xmlDoc.getDocumentElement();
 8if (xmlElmPerson == null ||
 9    !xmlElmPerson.getLocalName().equals("Person"))
10  throw new Exception("Must start with Person");
11
12Element xmlElmName = getFirstElement(xmlElmPerson);
13if (xmlElmName == null ||
14    !xmlElmName.getLocalName().equals("Name"))
15  throw new Exception("Missing Person->Name");
16
17Element xmlElmDOB = getNextElement(xmlElmName);
18if (xmlElmDOB == null ||
19    !xmlElmDOB.getLocalName().equals("DateOfBirth"))
20  throw new Exception("Missing Person->DateOfBirth");
21
22Element xmlElmAddress = getNextElement(xmlElmDOB);
23if (xmlElmAddress == null ||
24    !xmlElmAddress.getLocalName().equals("Address"))
25  throw new Exception("Missing Person->Address");
26
27
28Element xmlElmHouseNo = getFirstElement(xmlElmAddress);
29if (xmlElmHouseNo == null ||
30    !xmlElmHouseNo.getLocalName().equals("HouseNo"))
31  throw new Exception("Missing Person->Address->HouseNo");
32
33Element xmlElmPostCode = getNextElement(xmlElmHouseNo);
34if (xmlElmPostCode == null ||
35    !xmlElmPostCode.getLocalName().equals("PostCode"))
36  throw new Exception("Missing Person->Address->PostCode");
37
38if (getNextElement(xmlElmPostCode) != null)
39  throw new Exception("Unexpected Element found");
40
41System.out.println(
42  xmlElmName.getNodeValue() +
43  " was born " +
44  xmlElmDOB.getNodeValue() +
45  ", and lives at " +
46  xmlElmHouseNo.getNodeValue() +
47  ", " +
48  xmlElmPostCode.getNodeValue());
49
50Element xmlElmCar = getNextElement(xmlElmAddress);
51while (xmlElmCar != null)
52{
53  if (!xmlElmCar.getLocalName().equals("Car"))
54    throw new Exception("Unknown element ");
55
56  Element xmlElmMake = getFirstElement(xmlElmCar);
57  if (xmlElmMake == null ||
58      !xmlElmMake.getLocalName().equals("Make"))
59    throw new Exception("Missing Person->Car->Make");
60
61  Element xmlElmModel = getNextElement(xmlElmMake);
62  if (xmlElmModel == null ||
63      !xmlElmModel.getLocalName().equals("Model"))
64    throw new Exception("Missing Person->Car->Model");
65
66  if (getNextElement(xmlElmModel) != null)
67    throw new Exception("Unexpected Element found");
68
69  System.out.println(
70    " " + xmlElmMake.getNodeValue() +
71    ", " + xmlElmModel.getNodeValue());
72
73  xmlElmCar = getNextElement(xmlElmCar);
74}

75

 1private Element getFirstElement(Node xmlParent)
 2{
 3 if (xmlParent == null)
 4   return null;
 5 else if (xmlParent.FirstChild == null)
 6   return null;
 7 else if (xmlParent.getFirstChild() instanceof XmlElement)
 8   return (Element)xmlParent.getFirstChild();
 9 else
10   return getNextElement(xmlParent.getFirstChild());
11}

12
13private Element getNextElement(Node xmlNode)
14{
15 while (xmlNode != null)
16 {
17   xmlNode = xmlNode.getNextSibling();
18      if (xmlNode instanceof XmlElement)
19     return (XmlElement)xmlNode;
20 }

21 return null;
22}
Because the XML Data Binding code does the bulk of the parsing fo you, manipulating the XML objects is just a matter of dealing with collections of objects, no need to continually check the type of an item etc.

Conclusion

XML Data Binding makes it significantly easier to deal with XML documents from within your code, resulting in less code, which is simpler to read and maintain.

As the generated class library is strongly typed, it forms a kind of template for the developer, ensuring that the data created conforms the underlying XML Schema.

The maintenance phase of a project also befits as XML Data Binding allows any errors introduced because of changes in the data model to be caught early at compile time, unlike weekly typed DOM trees, which will give no indication of a problem until runtime. These kinds of changes can be a major cause of bugs which can only be caught in testing. Being able to identify these at compile time can save huge amounts of time and effort.

Liquid XML Studio

Starter Edition


Liquid XML Studio

Designer Edition


Liquid XML Studio

Developer Edition