问题描述
<complexType name="spThread">
<sequence>
<element name="SPThreadID" type="int" />
<element name="durtime" minOccurs="0" default="0">
<simpleType>
<restriction base="int">
<minInclusive value="0" />
</restriction>
</simpleType>
</element>
<element name="minexecutions" minOccurs="0" default="0">
<simpleType>
<restriction base="int">
<minInclusive value="0" />
</restriction>
</simpleType>
</element>
<element name="numThreads" type="int" />
<element name="procedures" type="spm:procedure" minOccurs="1"
maxOccurs="unbounded" />
</sequence>
</complexType>
我想用java代码生成这种类型的.xsd文件..?我该怎么做。?
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 ?
推荐答案
您可以利用现有的 xs,而不是创建自己的简单类型来表示以
类型。我将举例说明。 0
开头的整数: nonNegativeInteger
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
您可以使用 @XmlSchemaType
注释,用于指定应在XML模式中为字段/属性生成的类型。
You can use the @XmlSchemaType
annotation to specify what type should be generated in the XML schema for a field/property.
package forum11667335;
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;
}
}
演示
您可以在 JAXBContext
上使用 generateSchema
方法生成XML模式:
You can use the generateSchema
method on JAXBContext
to generate an XML schema:
package forum11667335;
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;
}
});
}
}
输出
以下是生成的XML架构。
Below is the XML schema that was generated.
<?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>
这篇关于如何使用java代码生成xsd文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!