This work is licensed using the "knowledge sharing signature"-"non-commercial use"-2.5 mainland China License Agreement in the same way.
Jaxb (Java architecture for XML binding) is a special serialization/deserialization tool. It allows XML data to be directly applied to Java programs in the form of Java objects, making conversion between Java objects and XML data possible. In jaxb, the conversion from Java objects to XML data is called marshal; the conversion from XML data to Java objects is called unmarshal.
Use jaxb in two steps. First, use the xjc command line program to compile the DTD or schema file to generate the JavaBeans class corresponding to the XML data structure, then, the marshans object and XML data are exchanged through the marshal and unmarshal operations in the jaxb API.
Jaxb uses the latest metadata technology, so it can only be used in JDK 1.5 or later versions. The official website of jaxb is as follows:
Https://jaxb.dev.java.net/
You can download the latest jaxb. The jaxb directory structure after installation is as follows:
/JAXB-HOME (path/to/jaxb)
/Bin
/Lib
/Docs
All the jaxb tools in the bin directory, including the xjc command line program, can be found in the lib directory; there is a jaxb help and API description in the docs directory.
I. xjc help
1. Compile through command line
The command line program xjc is used to compile a DTD or schema to generate a JavaBeans class corresponding to the XML data structure. The procedure is as follows:
Xjc [-XMLSCHEMA |-DTD] [-d directory for saving java files] [package where-P java files are located] <compilation File>
-XMLSCHEMA specifies that the format of the compiled file is schema. this parameter is the default value and is not required.
If this parameter is not provided, the compiling file must be a schema file.
-The DTD specifies that the format of the compiled file is DTD. If the compiled file is a DTD file, this parameter must be provided.
-D specifies the directory for saving the Java File
-P specifies the package of the generated Java File
For example, compile the schema.xls file under the current directory. the Java file is saved in the src directory and the package name is generate. The command is as follows:
Xjc-D Src-P generate schema. XSD
If the compiled file is schema. DTA, the command is as follows:
Xjc-DTD-D Src-P generate schema. DTD
2. Compile with ant
The JAXB-HOME package in the/jaxb-xjc.jar/lib directory contains the xjctask. Class file. You can define an ant Task Based on this class and generate a Java file by executing this task. The definition of the task is as follows:
<Taskdef name = "xjc" classname = "com. Sun. Tools. xjc. xjctask">
<Classpath>
<Fileset dir = "$ {JAXB-HOME}/lib" includes = "*. Jar"/>
</Classpath>
</Taskdef>
The ant code sample for xjc task execution is as follows:
<Target name = "generate" Description = "generate Java source files">
<Echo message = "generate Java from Schema..."/>
<Mkdir dir = "src"/>
<Xjc schema = "books. XSD" package = "com. Chris. jaxb" destdir = "gen-Src">
<Produces dir = "src/COM/Chris/jaxb" includes = "**/*. Java"/>
</Xjc>
</Target>
Where
Schema specifies the location of the DTD or schema file;
Destdir specifies the root directory of the generated Java file;
Package specifies the package of the generated Java file;
ARGs specifies additional parameters. The most important parameter is "-DTD". If the schema specifies a DTD file, "-DTD" must be provided in args ";
Produces is used to check the latest version in the specified directory.
Ii. serialization and deserialization)
To use jaxb in a Java program, you must use the following JAR packages:
Jaxb-api.jar
Jaxb-impl.jar
Activation. Jar
Jsr173_1.0_api.jar
The above packages can be found in the/JAXB-HOME/lib directory.
The core class of jaxb is the jaxbcontext class, which can load Java objects according to a package name or a class name.
The following example shows how to load Java objects with a package name:
Jaxbcontext JC = jaxbcontext. newinstance ("generate ");
The specified package must contain the objectfactory class, which is generated by xjc and used to generate the Java objects corresponding to the XML data.
If you use a class name to load a Java object, for example:
Jaxbcontext JC = jaxbcontext. newinstance (objectfactory. Class );
The specified class must be the factory class used to generate Java objects, such as the objectfactory class.
For ease of use, the following code encapsulates jaxb-related classes. The sample uses the package name loading mode and provides basic marshal and unmarshal operations.
Package generated; </P> <p> Import Java. io. inputstream; <br/> Import Java. io. outputstream; </P> <p> Import javax. XML. BIND. jaxbcontext; <br/> Import javax. XML. BIND. jaxbexception; <br/> Import javax. XML. BIND. extends aller; <br/> Import javax. XML. BIND. unmarshaller; </P> <p> public class jaxbutils {</P> <p> private jaxbutils () {</P> <p> throw new unsupportedoperationexception (); <br/>}</P> <p> Public static object unmarshal (string contextpath, inputstream xmlstream) <br/> throws jaxbexception {</P> <p> jaxbcontext JC = jaxbcontext. newinstance (contextpath); <br/> unmarshaller u = JC. createunmarshaller (); </P> <p> return U. unmarshal (xmlstream); <br/>}</P> <p> Public static void Marshal (string contextpath, object OBJ, <br/> outputstream Stream) throws jaxbexception {</P> <p> jaxbcontext JC = jaxbcontext. newinstance (contextpath); <br/> extends aller M = JC. createmarshaller (); <br/> M. setproperty (exploraller. jaxb_formatted_output, true); <br/> M. setproperty (exploraller. jaxb_encoding, "UTF-8"); </P> <p> M. marshal (OBJ, stream); <br/>}< br/>
This tool class is very simple and the code is as follows:
Public class jaxbtest {</P> <p> Public static void main (string [] ARGs) throws filenotfoundexception, <br/> jaxbexception {</P> <p> // unmarshal <br/> inputstream is = new fileinputstream ("C:/test. XML "); <br/> Object OBJ = jaxbutils. unmarshal ("generate", is); <br/> system. out. println (v. getuserid (); </P> <p> // Marshal <br/> outputstream F = new fileoutputstream ("C:/test1.xml"); <br/> jaxbutils. marshal ("generate", OBJ, f); <br/>}< br/>