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.
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.
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.
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.
1<?xml version="1.0" encoding="utf-16"?> 2<!--Created with Liquid XML Studio --> 3<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 4 <xs:element name="Person"> 5 <xs:complexType> 6 <xs:sequence> 7 <xs:element name="Name" type="xs:string" /> 8 <xs:element name="DateOfBirth" type="xs:date" /> 9 <xs:element minOccurs="0" name="Address"> 10 <xs:complexType> 11 <xs:sequence> 12 <xs:element name="HouseNo" type="xs:int" /> 13 <xs:element name="PostCode" type="xs:string" /> 14 </xs:sequence> 15 </xs:complexType> 16 </xs:element> 17 <xs:element minOccurs="0" maxOccurs="unbounded" name="Car"> 18 <xs:complexType> 19 <xs:sequence> 20 <xs:element name="Make" type="xs:string" /> 21 <xs:element name="Model" type="xs:string" /> 22 </xs:sequence> 23 </xs:complexType> 24 </xs:element> 25 </xs:sequence> 26 </xs:complexType> 27 </xs:element> 28</xs:schema>
1<?xml version="1.0"?> 2<Person> 3 <Name>Fred</Name> 4 <DateOfBirth>1978-06-26</DateOfBirth> 5 <Address> 6 <HouseNo>7</HouseNo> 7 <PostCode>WV6 6JY</PostCode> 8 </Address> 9 <Car>10 <Make>Escort</Make>11 <Model>Ford</Model>12 </Car>13 <Car>14 <Make>Elise</Make>15 <Model>Lotus</Model>16 </Car>17</Person> 18
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());
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}
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
Designer Edition
Developer Edition
Login or register (why?)
Shopping Cart