java生成xsd_java生成XSD示例

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 map = new HashMap();

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包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值