XML data binding framework jibx

Source: Internet
Author: User


XML data binding framework jibx


Author: Wang enjian
Statement: you are not allowed to reprint it without the consent of the author.

XML can be seen in many places in the Development of Java applications. For example, the most common thing is that we define parameter configuration files in XML file format to avoidProgram. Of course, the application of XML is not limited to this, and it also plays an important role in other technical fields. How to process XML files in Java? Currently, the most basic and underlying APIs are Dom, sax, JDOM, and dom4j. These APIs provide solutions for Java to process XML, but they do not have to use complicated methods and are prone to errors when processing XML. Remember that when cocoon 1.8 was used two years ago, because the legacy system used DOM to define the XML data file format, A large number of sub-elements, parent elements, peer-level elements, and attributes have not been figured out for a long time.

Jibx is an advanced API for processing XML. Similar projects include jaxb, Castor, and Zeus. When using this advanced API to process XML, your Java application will no longer have elements and attributes, instead of objects and Value domains. It is obvious that the difficulty of processing XML is greatly reduced, and it is more in line with object-oriented programming ideas.

I. Introduction to jibx

Jibx is a framework for binding XML data to Java objects. Jibx uses a binding definition document to define the conversion rules between XML data and Java objects. This document serves as a bridge between XML data and Java objects.

It is necessary to first introduce the two data binding terms marshal and unmarshal. Marshal is an XML file generated by the Java object, and unmarshal is a Java object created based on the XML file.

The jibx process is divided into two processes: Binding compiler and binding runtime. Binding compiler is a preliminary preparation process, including defining the binding definition file, defining the Java object bound with XML, and then compiling. In this process, jibx is easier to operate than other projects, without defining DTD and schema. The disadvantage is that you need to define your own Java program. Binding Runtime is to use the Java class compiled by binding compiler to process XML data.

Jibx also uses third-party tools xpp3 pull parser and bcel. files released by jibx also contain files related to these two tools.

2. Simple Example

By convention, we should start with the simplest example.

Download jibx from the jibx website. The latest version is beta 3. Undo the downloaded ZIP file, which contains a lib directory, contains bcel. jar, jibx-bind.jar, jibx-extras.jar, jibx-run.jar, xpp3.jar five jar files. Bcel. jar, jibx-bind.jar is used only when binding compiler. The jibx-extras.jar is an optional toolkit that contains some testing and validation tool classes.

1. Define an XML file to be processed. The file name is data. XML and the content is as follows:

<Customer>

<Person>

<Cust-num> 123456789 </Cust-num>

<First-name> JOHN </first-name>

<Last-name> Smith </last-name>

</Person>

<Street> 12345 happy Lane </street>

<City> plunk </city>

<State> wa </State>

<Zip> 98059 </zip>

<Phone> 888.555.1234 </phone>

</Customer>

This XML file is very simple and has 10 elements without attributes. The root element customer has six sub-elements: person, street, city, state, zip, and phone. The element "person" has three child elements: Cust-num, first-name, and last-name.

2. Define two Java classes "customer" and "person". The simplest method is to use the object's Domain value to correspond to elements. The content is as follows:

Public Class Customer {

Public person;

Public String Street;

Public String city;

Public String state;

Public integer zip;

Public String phone;

}

Public class person {

Public int customernumber;

Public String firstname;

Public String lastname;

}

There are no methods for these two classes, which is simple enough! As you can see, the seven fields of the customer class correspond to the seven child elements of the customer element in the XML file. The three fields in the person class correspond to the three child elements of the person element. The field Name of the person class is not exactly the same as that of the child element of the person element. This is required to comply with the field naming rules in Java. Although not equal, this is not important, you can match them one by one in the bound definition document.

3. Bind the definition document

The binding definition text block is the XML file bound to the XML data and Java object according to the binding definition specification. The file name is binding. XML and the content is as follows:

<Binding>

<Mapping name = "customer" class = "customer">

<Structure name = "person" field = "person">

<Value name = "Cust-num" field = "customernumber"/>

<Value name = "first-name" field = "firstname"/>

<Value style = "text" field = "lastname"/>

</Structure>

<Value name = "street" field = "street"/>

<Value name = "city" field = "city"/>

<Value name = "state" field = "state"/>

<Value name = "Zip" field = "Zip"/>

<Value name = "phone" field = "phone"/>

</Mapping>

</Binding>

The name and field attributes in the binding. xml file respectively correspond and bind the elements in XML to the fields in the Java object one by one.

<Mapping name = "customer" class = "customer">

The name and class attributes of the Mapping element bind the customer root element with the customer class.

<Structure name = "person" field = "person">

Public person;

The above two rows define that person is the field of customer, and also bind the person element with the person class.

4. Now let's look at the relationship between them, as shown in figure


Images from the official jibx website

For end users, they only know the XML data file and Java class, while binding the definition file is transparent.

5. Execute the binding compiler Process

The following command is executed in Linux. If it is a Windows platform, convert it to the appropriate command.

# Javac person. Java

# Javac-classpath. Customer. Java

# Java-jar lib/jibx-bind.jar binding. xml

After execution, four class files are added to the current directory: person. Class, customer. Class, jibx_bindingcustomer_access.class, and jibx_bindingfactory.class.

6. Execute the binding runtime Process

First, create an XML data file data. XML with the following content:

<Customer>

<Person>

<Cust-num> 123456789 </Cust-num>

<First-name> JOHN </first-name>

<Last-name> Smith </last-name>

</Person>

<Street> 12345 happy Lane </street>

<City> plunk </city>

<State> wa </State>

<Zip> 98059 </zip>

<Phone> 888.555.1234 </phone>

</Customer>

Next, write a simple test program test. Java to read data. xml. The content is as follows:

Import java. Io. fileinputstream;

Import java. Io. filenotfoundexception;

Import org. jibx. runtime. jibxexception;

Import org. jibx. runtime. ibindingfactory;

Import org. jibx. runtime. bindingdirectory;

Import org. jibx. runtime. iunmarshallingcontext;

class test {
Public static void main (string [] ARGs) {
try {
ibindingfactory bfact = bindingdirectory. getfactory (customer. class);
iunmarshallingcontext uctx = bfact. createunmarshallingcontext ();
customer = (customer) uctx. unmarshaldocument (New fileinputstream ("data. XML "), null);
person = customer. person;

System. Out. println ("Cust-num:" + person. mermernumber );

System. Out. println ("first-name:" + person. firstname );

System. Out. println ("last-name:" + person. lastname );

System. Out. println ("Street:" + customer. Street );

} Catch (filenotfoundexception e ){

System. Out. println (E. tostring ());

} Catch (jibxexception e ){

System. Out. println (E. tostring ());

}

}

}

Compile and run this test program

# Javac-classpath.: lib/jibx-run.jar test. Java

# Java-CP.: lib/jibx-run.jar: lib/xpp3.jar Test

The result of running the program is

Cust-num: 123456789

First-name: John

Last-Name: Smith

Street: 12345 happy Lane

Summary

In general, jibx is so simple, but jibx has a slightly more complex application. This article does not introduce it. Interested friends can continue to study it. If you have any questions, please go directly to our forum http://www.sentom.net/forum, and you are welcome to share your experience with us.

Original Author: Wang enjian

Origin: http://www.sentom.net

A total of 1760 readers have read this article

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.