使用SchemaOutputResolver將java檔案產生schema

來源:互聯網
上載者:User

標籤:jaxb   java2schema   

1.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="1.0")    static String version="1.0";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();}}
2.MySchemaOutputResolver.java
  <pre name="code" class="java">package com.soa.my.java2schema;import java.io.File;import java.io.IOException;import javax.xml.bind.SchemaOutputResolver;import javax.xml.transform.Result;import javax.xml.transform.stream.StreamResult;public class MySchemaOutputResolver extends SchemaOutputResolver {private File f;public MySchemaOutputResolver(String string, String string2) {f=new File(string, string2);}public Result createOutput(String namespaceUri, String suggestedFileName)throws IOException {return new StreamResult(f);}}

3.java2schemabyResolver.java

 

package com.soa.my.java2schema;import java.io.IOException;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.SchemaOutputResolver;public class java2schemabyResolver {/** * @param args * @throws JAXBException  * SchemaOutputResolver outputResolver; */SchemaOutputResolver outputResolver;public static void main(String[] args) throws JAXBException, IOException {        Class[] classes={ProductAnnoction.class};        new java2schemabyResolver().excute(classes);        }private void excute(Class[] classes) throws JAXBException, IOException {          JAXBContext context=JAXBContext.newInstance(classes);          context.generateSchema(outputResolver);} public java2schemabyResolver(){outputResolver=new MySchemaOutputResolver("e:/xml/xml練習/resolver","MySchema.xsd");}}

使用SchemaOutputResolver將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.