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