标签: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); } }
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
标签:jaxb java2schema
原文地址:http://blog.csdn.net/u013998070/article/details/44920925