dynamic management of XML schema variants in XMLBeans applications

Source: Internet
Author: User
Tags ibm developerworks
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.