1.[代码][Java]代码
import java.lang.reflect.Field;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(namespace="com.xml",name="Student")
@XmlType(namespace="com.xml")
public class Student {
@XmlElement(defaultValue="1.0")
static String verson = "1.0";
private String name;
private String sex;
private int age;
private String college;
private String grade;
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.[代码][Java]代码
import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public class XSDCreate {
SchemaOutputResolver resolver;
public XSDCreate(){
resolver = new MySchemaOutputResolver("/home/archermind", "MySchema.xsd");
}
public void execute(Class...classes){
try {
JAXBContext context = JAXBContext.newInstance(classes);
context.generateSchema(resolver);
System.out.println("All done");
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Class[] classes = {Student.class};
new XSDCreate().execute(classes);
}
}
class MySchemaOutputResolver extends SchemaOutputResolver{
private File f;
public MySchemaOutputResolver(String dir,String fileName){
f = new File(dir, fileName);
}
@Override
public Result createOutput(String namespaceUri, String suggestedFileName)
throws IOException {
return new StreamResult(f);
}
}