java xsd文件,如何使用java代码生成xsd文件?

java xsd文件,如何使用java代码生成xsd文件?maxOccurs unbounded gt iwanttogener xsdfileusing Howcanidotha Speciallyhow 解决方案 Insteadofcre

java xsd文件,如何使用java代码生成xsd文件?

maxOccurs=”unbounded” />

i want to generate this type of .xsd file using java code..? How can i do that.?

Specially how to generate Simple type elements and put restrictions on it ?

解决方案

Instead of creating your own simple type to represent integers starting with 0, you could leverage the existing xs:nonNegativeInteger type. I’ll demonstrate with an example.

SpThread

You can use the @XmlSchemaType annotation to specify what type should be generated in the XML schema for a field/property.

package forum;

import javax.xml.bind.annotation.XmlSchemaType;

public class SpThread {

private int durTime;

@XmlSchemaType(name=”nonNegativeInteger”)

public int getDurTime() {

return durTime;

}

public void setDurTime(int durTime) {

this.durTime = durTime;

}

}

Demo

You can use the generateSchema method on JAXBContext to generate an XML schema:

package forum;

import java.io.IOException;

import javax.xml.bind.*;

import javax.xml.transform.Result;

import javax.xml.transform.stream.StreamResult;

public class Demo {

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;

}

});

}

}

Output

Below is the XML schema that was generated.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/228408.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月16日 下午7:05
下一篇 2026年3月16日 下午7:06


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号