The Apache XMLBeans itself does not support multiple XML schema versions. This is a big limitation for applications that need this support to implement compatibility management. But this problem can be solved. In this article, you will learn how dynamic class loading techniques can help solve problems.
The Apache XMLBeans is an open-source, XML-and Java™-bound tool that can be used to generate Java classes and interfaces from the XML schema. Using the generated beans, you can parse or generate XML documents that follow the pattern. Thus, this binding binds the generated Java class and the XML schema tightly together. When you perform a large or small modification to an XML schema, the bean is regenerated and the new bean that corresponds to the modified XML schema is used. At the very least, it is trying to achieve this. Unfortunately, applications sometimes need to support multiple schema versions. For example, if you use XML as a data interchange standard, your application must provide forward and backward compatibility to support newer or older version standards.
Environment settings
Consider an employee data management application in which the application saves employee information in the form of an XML file and uses Apache XMLBeans for processing. The employee data is always defined by the XSD, as shown in Listing 1. Schema is compiled using the XMLBeans schema compiler. The application then uses the generated Java classes and interfaces to process the incoming XML document, which follows the XSD, and the namespace is com.ibm.sample.employee:1.
Listing 1. The first version of the employee XML schema
<?xml version= "1.0" encoding= "UTF-8"?>
xmlns= "Http://www.w3.org/2001/XMLSchema" xmlns:tns= "Com.ibm.sample.employee:1" >
<element name= "Employee" type= "Tns:employeetype" ></element>
<complextype name= "Employeetype" > <sequence> <element name= "FirstName" type= "string" ></element> <element name= "LastName" type= "string" ></element> <element name= "department" type= "string" ></element> <element name= "Phone" type= "string" ></element> <element name= "Eemail" type= "string" ></element> </sequence> </complexType> </schema>
|
Listing 2 shows an application that handles employee XML, listing 3 shows the client application that uses it:
Listing 2. XMLBeans Applications
public class Employeeapplication {
public void Parsexml (String xmlfilename) {
EmployeeDocument.Factory.parse (new File (XML)); Employeetype employee = Employeedoc.getemployee (); System.out.println (Employee.getfirstname ()); Do something more useful with the employee data
} }
|
Listing 3. Client
Employeeapplication app = new Employeeapplication (); App.parsexml (Xmlfilepath);
|
Introduction to Schema Changes
Now that the schema is changed, the Java Bean generated for the old schema can no longer handle XML with the new schema. Listing 4 shows the modified pattern, where new elements are added, and the namespace is changed to Com.ibm.sample.employee:2, which means that this is the second version.
Listing 4. Second version of the employee XML schema
This article has been transferred from IBM Developerworks China
Please click here to view the full text