Java and XML story one: Generating Java classes from XSD

Source: Internet
Author: User

JAXB makes the tedious mechanical coding between Java and XML the past, and now it's easy to generate the corresponding Java file through an XSD file, and I typically use maven-jaxb2-plugin to implement XSD-to-Java file generation.
POM Configuration
Specify the XSD file path, set the binding file path, and specify the path to generate Java files

<plugin>  <groupId>Org.jvnet.jaxb2.maven2</groupId>  <artifactid>Maven-jaxb2-plugin</artifactid>  <version>0.9.0</version>  <executions>    <execution>      <ID>Schema1-generate</ID>      <goals>        <goal>Generate</goal>      </goals>      <configuration>       <schemadirectory>Src/main/resources/dir1</schemadirectory>        <schemaincludes>          <include>1.xsd</include>        </schemaincludes>        <extension>True</extension>        <generatedirectory>${project.build.directory}/generated-sources/xjc1</generatedirectory>        <episodefile>${project.build.directory}/generated-sources/xjc1/meta-inf/1.episode</episodefile>          <bindingdirectory>Src/main/resources/dir1</bindingdirectory>        <bindingincludes>             <include>1.xjb</include>        </bindingincludes>      </configuration>    </Execution>  </executions></plugin>

BINDING.XJB Configuration
Implement type matching, package matching, and if type matching is complex, you may need to implement your own conversion class

<jaxb:bindings xmlns:jaxb="Http://java.sun.com/xml/ns/jaxb" XMLNS:XJC ="HTTP://JAVA.SUN.COM/XML/NS/JAXB/XJC"xmlns:xs="http://www.w3.org/2001/ XmlSchema " version=" 2.0 " schemalocation=" Book.xsd ">        <jaxb:globalbindingsfixedattributeasconstantproperty= "true" typesafeenumbase= "xs:string" Typesafeenummembername= "Generatename" generateissetmethod= "true"><!--<xjc:novalidator/><xjc:novalidatingunmarshaller/>-->        <xjc:serializable uid="1" />        <jaxb:javatype name="Java.util.Calendar" xmlType="Xs:datetime"  Parsemethod= "javax.xml.bind.DatatypeConverter.parseDateTime" printmethod=" Javax.xml.bind.DatatypeConverter.printDateTime " />    </jaxb:globalbindings>        <jaxb:schemabindings schemalocation= "1.xsd" node="/xs: Schema ">             <jaxb:package name="Com.cloud" />     </jaxb:schemabindings>  </jaxb:bindings>

Note1: The new version of plugin can not only generate Java class, but also help generate the most important methods of object: ToString, Equals, Hashcode, clone, Just configure the plugin on the configuration.

<configuration>    <extension>True</extension>    <args>        <arg>-xtostring</arg>        <arg>-xequals</arg>        <arg>-xhashcode</arg>        <arg>-xcopyable</arg>    </args>    <plugins>        <plugin>            <groupId>Org.jvnet.jaxb2_commons</groupId>            <artifactid>Jaxb2-basics</artifactid>            <version><!--version 0.9.0--></version>        </plugin>    </plugins></configuration>

Note2: I have encountered the same project in 2 XSD files have the same node type, different content, although the different packages are specified to generate source or error, The final solution is to parse the different XSD files with 2 execution, but I don't have this problem with the CXF plugin, so I can generate all the Java classes I want in a execution.

Java and XML story one: Generating Java classes from XSD

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.