使用SchemaOutputResolver将java文件生成schema

本文介绍了一种使用Java注解自动生成XML Schema的方法。通过示例代码展示如何定义Java类并利用JAXB进行Schema输出,同时实现了自定义输出解析器以指定Schema文件的保存路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
	}

	

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值