标签:jaxb java2schema
1.编写一个自己需要的java类
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.下载xjc工具,可以到下面的链接进行下载:
http://download.csdn.net/detail/u013998070/8569667
3.打开命令行,调整到xjc工具包的bin 目录
如果不清楚需要输入什么,可以直接输入Schemagen命令进行查看,如下
4.在命令行输入相关指令,如下图
5.生成的schema.xsd文件内容如下:
<?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.结果分析,从上面的schema中我们可以看出,该schema文件并没有命名空间;而且生成的是complexType
而不是element,要想改变该问文件的内容,则需要对product.java使用jaxb注解
productAnnoction.java如下
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(); } }
<?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>
标签:jaxb java2schema
原文地址:http://blog.csdn.net/u013998070/article/details/44920139