Chinese translation of official jaxb documents

Source: Internet
Author: User
Tags unsupported

Jaxb provides APIs and tools for automatically ing XML documents and Java objects.
The jaxb framework can complete the following operations:
1. unmarshal the XML content into Java Representation
2. Access and update the Java Representation
3. Java representation of XML content into XML content

 
Jaxb provides efficient and standard ing between XML and Java code. Java developers can use jaxb to simplify development and improve efficiency, because jaxb can write a small amount of code without being proficient in XML. Jaxb makes it easy for developers to use XML and Web Service technologies to expand applications.
1. Release Notes
Jaxb reference implementation must run on j2se 5.0 or a higher Java platform. The jar file is as follows:
2.0 runtime environment: the jar files required for the deploy jaxb2.0 client include jaxb-api.jar, jaxb-impl.jar, jsr173-1.0_api.jar
1.0 runtime environment: the jar files required for the deploy jaxb1.0 client include 2.0 Runtime Environment + jaxb1-impl.jar

2. xjc Tool
Although the xjc script file is used in other articles of the author, in order to maintain the integrity of the article, the content of the xjc script is appended to this article.
Binding schema indicates generating a series of Java classes, which represent schema. All jaxb implementations provide a tool called the binding compiler to bind a schema. For example, the jaxb reference implementation provides a binding compiler that allows script execution (shell/BAT ). Assume that you need to bind the books. XSD file and assume that the file is working with * nix, the binding command is as follows:
Xjc. Sh-P test. jaxb books. XSD-D work

Option description:
-P: Specifies the package name of the generated class.
-D: Specifies the target directory for storing classes.
(Note: there are other options and methods to bind the schema, such as the xjc plug-in of eclispe)
C:/Documents and Settings/hp3643> xjc -- Help

Usage: xjc [-options...] <schema file/URL/dir>... [-B <bindinfo>]...
Options:
-NV: do not strictly check the input schema. By default, xjc performs a strict schema check. You can use this option to do not strictly check the schema, but not strictly check the input schema.
-Extension: by default, xjc binds the compiler to enforce the compatibility rules in the jaxb specification. Appendix E.2 defines W3C XML Schema features that are not supported by some columns of jaxb V1.0. In this case, you can use the-extension mode to support these features. By default, only custom bindings defined in the Specification can be used. You can use the jaxb provider extension by using the-extension option.
-B <File>: Specifies the external binding file (each <File> must have its own-B, which is irrelevant to the sequence of appearance)
Xjc schema1.xsd schema2.xsd schema3.xsd-B bindings123.xjb
Xjc schema1.xsd schema2.xsd schema3.xsd-B bindings1.xjb-B bindings2.xjb-B bindings3.xjb
-D <dir>: The target path of the generated file. By default, Java content classes are generated in the current directory. The target directory must already exist. xjc will not automatically create these directories.
-P <PKG>: Specifies the target package name.
-Httpproxy <proxy>: sets the HTTP/HTTPS proxy in the format of [user [: Password] @] proxyhost [: proxyport]
-Classpath <Arg>: Specifies the user's class path.
-Catalog <File>: Specifies a category file, which is used to process external entity applications. tr9401, xcatalog, and oasis xml catalog formats are supported.
-Readonly: the status of the generated file is read-only.
-NPA: does not generate package-level annotations
-XMLSCHEMA: process input as W3C XML Schema (default)
-Relaxng: the input is processed as relaxng (not supported in the test phase ).
-Relaxng-compact: the input is treated as the relaxng comapact syntax (not supported in the test phase ).
-DTD: treats the input as an xml dtd (experimental, unsupported)
-WSDL: processing input as WSDL (experimental, unsupported)
-Verbose: additional display
-Quiet: No compilation results are output.
-Help: displays help information.
-Version: displays version information.

Extensions:
-Xlocator: enables source code location support for generated code
-Xsync-Methods: indicates that the access method flag is ''synchronized '.
-Mark-generated: Mark the generated code as @ javax. annotation. generated
Run the command in Windows: F:/Eclipse/workspace/jaxbtutorial/src> xjc-P test. jaxb-D. Text/JAXP/book. XSD
Java (TM) Web Services developer pack 2.0 is installed in the translator's system, the installation directory is C:/Sun/jwsdp-2.0, and C: /Sun/jwsdp-2.0/jaxb/bin added to the PATH environment attribute of the system)
Parsing a schema...
Compiling a schema...
Test/jaxb/bookcategorytype. Java
Test/jaxb/booktype. Java
Test/jaxb/collection. Java
Test/jaxb/objectfactory. Java
After running the preceding command, some classes are generated.

3. xjc ant task
The jaxb-xjc.jar file is included in jwsdp or other jaxb Ri libraries, including the xjctask. Class file, which can be applied to ant build tools. The ant build script (generally build. XML) contains the following statements:
<Taskdef name = "xjc" classname = "com. Sun. Tools. xjc. xjctask">
<Classpath>
<Fileset dir = "path/to/jaxb/lib" includes = "*. Jar"/>
</Classpath>
</Taskdef>
The preceding task definition maps xjctask to ant task and name it xjc. The example contains how to use xjc tasks.
The task syntax is as follows:
Environment variable:
Ant opts-PASS command line parameters to JVM. For example, define system properties or set the maximum Java heap size.
Parameter attributes:
Xjc supports the following parameter attributes:
Schema: XML schema file. This attribute or the <schema> sub-element must contain one.
Binding: an external binding file, which is applied to a schema file.
Package: if specified, the generated Java file will be placed under this package, which is the same as the command line parameter-P.
Destdir: target directory, which is the same as the command line parameter-D. Required.
Readonly: whether to generate the Java source file in read-only mode. The default value is false.
Extension: if it is set to true, the xjc binding table will run in extended mode together. Otherwise, it will run in strict mode. The parameter is the same as the-extension parameter of the command line tool.
Catalog: Specifies a category file to process external entity references.
Removeoldoutput: and <produces> subelements are accepted. When set to yes, the file pointed to by the <produces> element will be deleted before the xjc compiler is run.
Source: the version of the compiler to use. The generated Java code may be 1.0 or. refer to the rules set by jaxb1.0 or jaxb2.0.
Xjc supports the following nested elements:
Schema: compile one or more schemas at the same time. the syntax of Schema sub-elements is the same as that of <fileset>.
Binding: You can specify one or more external binding files at the same time. The syntax of the binding sub-element is the same as that of <fileset>.
Classpath: Specifies the class path specified by the user.
Arg: some additional command line parameters. This element can specify multiple options:
-NV
-Use-runtime
-Schema
-DTD
-Relaxng
-Xlocator
-Xsync-methods
Here are some examples of using xjc task:
Compile src/myschema. xmd. The target directory is SRC and the package name is org. Acme. Foo:
<Xjc schema = "src/myschema. XSD" target = "src" package = "org. Acme. foo"/>
Compile All Schema files under SRC. The target directory is SRC, and the package name is default:
<Xjc target = "src">
<Schema dir = "src" includes = "*. XSD"/>
</Xjc>
Compile All Schema files under SRC. The target directory is SRC and bind it to all xjb files under the src directory:
<Xjc target = "src">
<Schema dir = "src" includes = "*. XSD"/>
<Binding dir = "src" includes = "*. xjb"/>
</Xjc>
Because ant requires folders in fileset by default, mkdir is required. The following example shows a up-to-date check if ABC. if the XSD is updated, the files in the src/org/Acme/Foo and impl subdirectories will be deleted before compilation, therefore, do not place your own class files in the preceding two directories.
<Mkdir dir = "src/org/Acme/foo"/>
<Xjc target = "src" schema = "ABC. XSD" removeoldoutput = "yes" package = "org. Acme. foo">
<Produces dir = "src/org/Acme/foo" includes = "* impl/*"/>
</Xjc>
For more complex up-to-date checks, if the XSD is updated or the DTD file is updated, the schema must be re-compiled.
<Mkdir dir = "src/org/Acme/foo"/>
<Xjc target = "src" removeoldoutput = "yes" package = "org. Acme. foo">
<Schema dir = "schema" schemdes = "*. XSD"/>
<Depends dir = "schema" schemdes = "*. DTD"/>
<Produces dir = "build/generated-src/org/Acme/foo" includes = "**/*"/>
</Xjc>
Use Arg sub-element:
<Xjc target = "src">
<Schema dir = "src" includes = "**/*. XSD" excludes = "**/debug. XSD"/>
<Arg value = "-NV"/>
</Xjc>
Set environment variables or system attributes:
> Set ant_opts =-dhttp. proxyhost = webcache. East
> Set ant_opts = % ant_opts %-dhttp. proxyport = 8080
> Ant

4. schemagen Tool
The schema generator can run through schemagen shell scripts or bat batch files. Currently, the schema generator can process Java source files or class files.
At the same time, ant task is also provided to run the schema generator.
An example of using this tool for different operating systems is as follows:
For Solaris/Linux
% PATH/to/jaxb/bin/schemagen. Sh Foo. Java bar. Java...

For WindowsNT/2000/XP
> Path/to/jaxb/bin/schemagen. Bat Foo. Java bar. Java...
Note: The default schema file name is schema1.xsd.
If you need to refer to other classes for Java source files or class files, you must use the system classpath or specify the-classpath option. Otherwise, an error occurs.
Command line parameters:
Usage: schemagen [Options...] <java files>
Options:
-D <path>: Specifies the directory in which class files generated by the processor and javac are stored.
-CP <path>: Specifies the directory of the file.
-Classpath <path>: Specifies the directory of the file.
-Help: displays help information.
Currently, the schema generator generates a schema file for the namespace used by Java class files. There is no way to control the name of the generated schema file. You can use the ant task to complete this function.

5. Ant task of the schema Generator
The jaxb-xjc.jar file contains the schemagentask. Class file so that you can use the schema generator in the ant build tool. The definition of schemagentask is as follows:
<Taskdef name = "schemagen" classname = "com. Sun. Tools. jxc. schemagentask">
<Classpath>
<Fileset dir = "path/to/jaxb/lib" includes = "*. Jar"/>
</Classpath>
</Taskdef>

 

Article Source: http://www.diybl.com/course/3_program/java/javajs/200853/112204.html



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.