XML is in the development of Java applications, many places can see its figure, such as the most common thing to define the parameter configuration file is the XML file format, avoid hardcoding of the program. Of course, the application of XML is not limited thereto, but also in other technical fields also occupies a pivotable position. How to process XML files in Java? At present, the most basic API is DOM, SAX, JDOM, DOM4J. These APIs provide solutions for Java processing XML, but insufficient use of their method, it is easy to have errors when processing XML. I remember when using Cocoon 1.8 two years ago, because the legacy system uses the DOM definition XML data file format, a large number of child elements, parent elements, peer elements, and attributes make people have a good time.
This time we introduce JIBX is the advanced API of XML, and the same type of projects also have Jaxb, Castor, Zeus. Using such advanced APIs, in processing XML, your Java app will no longer have elements and properties, replaced by objects and value domains. The advantage is obvious, and the difficulty of processing XML is greatly reduced, and more complies with object-oriented programming ideas.
A JIBX introduction
JIBX is a framework that binds XML data to Java objects. JIBX uses a binding definition document to define the rules of XML data and Java object transition. This text is the bridge between XML data and Java objects.
It is necessary to introduce two data binding terms, Marshal and Unmarshal, Marshal generate an XML textbook by the Java object, and Unmarshal is based on the XML text.
Dividing the process using JIBX into two processes, one is binding compiler, and the other is Binding Runtime. Binding Compiler is a preparation process, including defining binding definitions, defined Java objects that are bound to XML, and then compile. In this process, JIBX is simpler than other items, without defining DTD and Schema, the disadvantage is that you need yourself to define Java programs. Binding Runtime is a Java Class handled XML data using Binding Compiler.
JIBX also uses third-party tools XPP3 Pull Parser and BCEL, which also contains the two tools related files in the files published in the JIBX.
Second simple example
According to conventions, it is still from the simplest example.
First download JIBX from the JIBX website, the current latest version is Beta 3. Unwrite the downloaded ZIP file, there is a lib directory, including bcel.jar, jibx-bind.jar, jibx-extras.jar, jibx-run.jar, Xpp3.jar five JAR files. Bcel.jar, jibx-bind.jar is only available when Binding Compiler is available. JIBX-Extras.jar is an optional toolkit with some tests and validation tool classes.
1. Define a file we will have to process the XML file, the file name is DATA.XML, the content is as follows:
Customer>
This XML file is very simple, with ten elements, no attributes. The root element Customer has Person, Street, City, State, ZIP, Phone six sub-elements. The element Person has three sub-elements of Cust-Num, First-Name, Last-Name.
2. Next, two Java class Customer and Person are defined, and the simplest way, with the object's domain value, the content is as follows:
Public class customer {
Public Person 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;
}
This two classes do not have any ways, simple enough! Perhaps you have seen it, the seven field of the Customer class correspond to seven sub-elements of the Customer element in the XML file. The three fields of the Person class correspond to three sub-elements of the Person element. The name of the Field of the Person class is not completely equal to the child element name of the Person element. This is the need to comply with the Java programming specification specification, although it is not equal, this is not important, you can put them in the binding definition. One correspondence.
3. Binding definition text
Binding definitions The XML text binding XML data and Java objects are bound by binding definition specification. The file name is binding.xml, the content is as follows:
structure>
mapping>
binding>
The Name and Field properties in the binding.xml file are each corresponding to the elements in XML and the field in the Java object, and bind them.
The name and class attribute of the mapping element are bound to the Customer root element and the Customer class.
Public Person Person;
The above two lines define Person's Field, and also bind the Person elements and Person classes.
4. Let's take a look at the relationship between them, as shown below
Image from JIBX official website
For end users, they only know the XML data text and Java classes, and the binding definition text is transparent.
5. Execute a Binding Compiler process
The following command is executed under Linux, if it is a Windows platform, please convert into a corresponding command
#javac person.java
#javac -classpath. Customer.java
#java -jar lib / jibx-bind.jar binding.xml
After the execution, there are more Class files in the current directory, namely Person.class, Customer.class, Jibx_BindingCustomer_Access.class, Jibx_BindingFactory.class.
6. Execute the Binding Runtime Process
First customize an XML data file DATA.XML, the content is as follows:
persons>
Customer>
Next, write a simple reading of the Data.xml test program Test.java, 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 = (Customer) Uctx.unmarshalDocument (New FileInputStream ("DATA.XML"), NULL)
Person Person = Customer.Person;
System.out.println ("Cust-Num:" Person.customernumber);
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 (jibxext 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
Note: The above Compile seems to be a bit problem, I have improved it, as follows:
Run.bat:
CLS set classlib = lib / bcel.jar; lib / jibx-bind.jar; lib / jibx-extras.jar; lib / jibx-run.jar; lib / xpp3.jarset classpath =.% localclasspath%;% classlib%; Set java_bin = c: /j2sdk1.4.1_02/bin% java_bin% / javac person.java Customer.java% java_bin% / java -classpath lib / jibx-run.jar -jar lib / jibx-bind.jar binding.xml% Java_bin% / javac -classpath% classpath% - SourcePath Customer jibxtestmain.java% java_bin% / java -cp% classpath% JIBXTESTMAIN
Summary
In general, JIBX is so simple, but JIBX has a slightly complicated application. This article has not been introduced, and interested friends can continue to study. If you have any questions, you can go directly to our forum http://www.sentom.net/forum, and very welcome to share your experience.