Data binding JAXB

Source: Internet
Author: User
Tags echo message

JAXB (Java architecture for XML Binding) provides a quick and easy way to bind XML Schemas and Java, making it easy for Java programmers to work with XML data in Java applications. JAXB provides a way to group XML documents into Java content trees, and to regroup Java content trees back into XML documents. JAXB also provides a way to generate XML schemas from Java objects.

Here are a few important definitions:

Marshalling (marshalling) is the process of converting data in memory into a storage medium. So in the Java and XML environment, grouping is translating some Java objects into one (or more) XML document. In a database environment, the data that is represented in Java is stored in the database. Obviously, the secret of marshalling is to translate the object-oriented structure in the Java instance into a flat structure that applies to XML, or the relational structure in the RDBMS (the conversion to OODBMS using Java technology is actually very simple).

The solution group (unmarshalling) is the process of converting data from a storage medium to memory-just as opposed to marshalling. Therefore, XML documents need to be extracted into Java VM. The complexity here is not in flat data because it is not required, but in mapping from the right data to the correct Java code variable. If the mappings are wrong, it is not possible to access the data correctly. Of course, if you try to regroup again, it can cause a bigger problem, and the problem spreads fast.

Round-trip (round-tripping) is probably the most important and most misunderstood data binding term. A round trip is used to describe a complete loop from storage media to memory and then back to the storage medium. In the context of XML and Java technology, this means from XML documents to Java instance variables, and then back to XML documents. The correct round-trip requirements, if the data is not modified in the middle, the XML input and XML output should be equivalent.

To generate a Java file:

1. The build operation can be performed through the XJC in the Jaxb bin, as follows:

Xjc–p COM.CHRIS.JAXB test.xsd–d gen-src

-P <PKG>: Specifying package for generating Java files

-D <dir>: Specifies the directory where Java files are generated

The directory specified by-D must already exist, otherwise it will be reported as "cowardly refuses to write to a non-existent directory" Gen-src ".

2. Using Ant Build

Define a XJC task in the ant file that generates the Java file and the ant file definition by executing the task:

<property name="jaxb.home" value="./lib" />
  <path id="classpath">
  <pathelement path="src" />
  <fileset dir="${jaxb.home}" includes="*.jar" />
</path>
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
  <classpath refid="classpath" />
</taskdef>
<target name="generate" description="generate Java source files">
  <echo message="generate java from schema..." />
  <mkdir dir="gen-src" />
  <xjc schema="test.xsd" package="com.chris.jaxb" destdir="gen-src">
    <produces dir="gen-src/com.chris.jaxb " includes="**/*.java" />
  </xjc>
</target>

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.