使用Schemagen從java類變成schema

來源:互聯網
上載者:User

標籤: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>


   


使用Schemagen從java類變成schema

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.