第一步 编写javaBean,使用@XmlSchemaType批注指定在XML模式中为字段/属性生成的类型.
public class SpThread {
private int durTime;
@XmlSchemaType(name="nonNegativeInteger")
public int getDurTime() {
return durTime;
}
public void setDurTime(int durTime) {
this.durTime = durTime;
}
}
第二步 使用JAXBContext进行转换
import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public class BeanToXmlSchema {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(SpThread.class);
jc.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
StreamResult result = new StreamResult(System.out);
result.setSystemId(suggestedFileName);
return result;
}
});
}
}
输出
<?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="spThread">
<xs:sequence>
<xs:element name="durTime" type="xs:nonNegativeInteger"/>
</xs:sequence>
</xs:complexType>
</xs:schema>