import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
 
import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.XmlSchemaAll;
import org.apache.ws.commons.schema.XmlSchemaCollection;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaGroupBase;
import org.apache.ws.commons.schema.XmlSchemaObjectCollection;
import org.apache.ws.commons.schema.constants.Constants;
import org.apache.ws.commons.schema.utils.NamespaceMap;
 
public class GenXSD {
 
public static void main(String[] args) throws IOException {
 
XmlSchema schema = new XmlSchema();
String targetNamespace = null;
schema = new XmlSchema(targetNamespace, null, new XmlSchemaCollection());
HashMap<String, String> map = new HashMap<String, String>();
schema.setNamespaceContext(new NamespaceMap(map));
 
XmlSchemaObjectCollection schemaCollection = schema.getItems(); // 得到schema中的集合
/**
* 根元素
*/
XmlSchemaElement boRootComplexElement = new XmlSchemaElement();
boRootComplexElement.setAnnotation(null);
boRootComplexElement.setName("BORoot");
 
schemaCollection.add(boRootComplexElement);
 
XmlSchemaComplexType rootComplexType = new XmlSchemaComplexType(schema);
XmlSchemaGroupBase rootParticleAll = new XmlSchemaAll();
XmlSchemaObjectCollection rootParticleCollection = rootParticleAll
.getItems();
rootComplexType.setParticle(rootParticleAll);
 
boRootComplexElement.setSchemaType(rootComplexType);
 
/**
* 表Element
*/
 
XmlSchemaElement userComplexElement = new XmlSchemaElement();
userComplexElement.setAnnotation(null);
userComplexElement.setName("User");
 
XmlSchemaElement posComplexElement = new XmlSchemaElement();
posComplexElement.setAnnotation(null);
posComplexElement.setName("Profile");
 
rootParticleCollection.add(userComplexElement);
rootParticleCollection.add(posComplexElement);
/**
* 简单类型
*/
// XmlSchemaSimpleType schemaSimpleType = new
// XmlSchemaSimpleType(schema);
 
/**
* 复杂类型
*/
XmlSchemaComplexType schemaComplexType = new XmlSchemaComplexType(
schema);
XmlSchemaGroupBase particleAll = new XmlSchemaAll();
XmlSchemaObjectCollection particleCollection = particleAll.getItems();
 
XmlSchemaElement nameElement = new XmlSchemaElement();
nameElement.setName("name");
nameElement.setSchemaTypeName(Constants.XSD_STRING);
 
XmlSchemaElement ageElement = new XmlSchemaElement();
ageElement.setName("age");
ageElement.setSchemaTypeName(Constants.XSD_INT);
 
XmlSchemaElement birthDayElement = new XmlSchemaElement();
birthDayElement.setName("birth");
birthDayElement.setSchemaTypeName(Constants.XSD_DATE);
 
particleCollection.add(nameElement);
particleCollection.add(ageElement);
particleCollection.add(birthDayElement);
 
schemaComplexType.setParticle(particleAll);
userComplexElement.setSchemaType(schemaComplexType);// 在Element中添加complexType
 
File file = new File("c:/sample.xsd");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (file.exists()) {
file.delete();
file.createNewFile();
}
FileOutputStream fout = new FileOutputStream(file);
schema.write(fout);
fout.flush();
 
if (fout != null) {
fout.close();
}
 
}
 
}
 
需要导入XmlSchema-1.4.2.jar包