Use Schemagen to change from Java class to schema

Source: Internet
Author: User

1. Write a Java class that you need

Package Com.soa.my.java2schema;public class Product {private static final long serialversionuid=12345l;private String name;private int price;public String getName () {return name;} public void SetName (String name) {this.name = name;} public int GetPrice () {return price;} public void Setprice (int. price) {this.price = Price;} Public Product () {super ();}}

2. Download the XJC tool, which can be downloaded at the following link:

http://download.csdn.net/detail/u013998070/8569667

3. Open the command line and adjust to the bin directory of the XJC Toolkit

If you do not know what you need to enter, you can simply enter the Schemagen command to view it, as follows

4. Enter the relevant instructions at the command line, such as

5. The contents of the generated schema.xsd file are as follows:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"? ><xs:schema version= "1.0" xmlns:xs= "/http Www.w3.org/2001/XMLSchema ">  <xs:complextype name=" Product ">    <xs:sequence>      <xs: Element name= "name" type= "xs:string" minoccurs= "0"/>      <xs:element name= "Price" type= "Xs:int"/>    < /xs:sequence>  </xs:complexType></xs:schema>

6. Results analysis, from the above schema, we can see that the schema file does not have a namespace, and the resulting complextype

Instead of element, to change the content of the asked file, you need to use JAXB annotations for Product.java

Productannoction.java as follows

Package Com.soa.my.java2schema;import Javax.xml.bind.annotation.xmlelement;import Javax.xml.bind.annotation.xmlrootelement;import Javax.xml.bind.annotation.XmlType; @XmlRootElement (name= " ProductAnnoaction1 ", namespace=" my.soa.java2schema.com ") @XmlType (namespace=" my.soa.java2schema.com ") public class productannoction {    private static final long serialversionuid=12345l;    @XmlElement (defaultvalue= "Gzy") private string name;private int price;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int GetPrice () {return price;} public void Setprice (int. price) {this.price = Price;} Public Productannoction () {super ();}}

Run the command line and the resulting file is as follows:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"? ><xs:schema version= "1.0" targetnamespace= " My.soa.java2schema.com "xmlns:tns=" my.soa.java2schema.com "xmlns:xs=" Http://www.w3.org/2001/XMLSchema ">  <xs:element name= "ProductAnnoaction1" type= "tns:productannoction"/> <xs:complextype  name= " Productannoction ">    <xs:sequence>      <xs:element name=" version "Type=" xs:string "default=" 1.0 " minoccurs= "0"/>      <xs:element name= "name" type= "xs:string" minoccurs= "0"/>      <xs:element name= " Price "type=" Xs:int "/>    </xs:sequence>  </xs:complexType></xs:schema>



Use Schemagen to change from Java class to schema

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.