从java类生成xsd的实用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9681373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
utility to generate xsd from java class
提问by Julias
I want to generate xsd for the following class
我想为以下类生成 xsd
public class Node{
private String value;
private List<Node> childrens;
}
What is the best utility to generate xsd schema for such code
为此类代码生成 xsd 架构的最佳实用程序是什么
In general I want to implement simple tree. I'm already using jaxb for generating the classes from schema.
一般来说,我想实现简单的树。我已经在使用 jaxb 从模式生成类。
采纳答案by nwinkler
If you're already using JAXB, you can use the schemagen
tool for creating an XSD:
如果您已经在使用 JAXB,则可以使用该schemagen
工具来创建 XSD:
- http://docs.oracle.com/javase/6/docs/technotes/tools/share/schemagen.html
- http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.express.doc%2Finfo%2Fexp%2Fae%2Ftwbs_jaxbjava2schema.html
- http://docs.oracle.com/javase/6/docs/technotes/tools/share/schemagen.html
- http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.express.doc%2Finfo%2Fexp%2Fae%2Ftwbs_jaxbjava2schema.html
There are also Ant tasks and Maven plugins for doing the same in an automated fashion.
还有 Ant 任务和 Maven 插件可以以自动化方式执行相同的操作。
回答by bdoughan
You can use the generateSchema
API on JAXBContext
to generate an XML schema:
您可以使用generateSchema
API onJAXBContext
生成 XML 模式:
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(Node.class);
jc.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String namespaceURI, String suggestedFileName)
throws IOException {
return new StreamResult(suggestedFileName);
}
});
}
}
回答by Lonzak
There are also Ant tasks and Maven plugins for doing the same in an automated fashion.
还有 Ant 任务和 Maven 插件可以以自动化方式执行相同的操作。
Yes indeed there are. Before you have to figure it out for yourself here is the maven version:
是的,确实有。在你必须自己弄清楚之前,这里是 maven 版本:
(1) Add the maven plugin to your pom.xml
(1)在你的pom.xml中添加maven插件
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>schemagen</id>
<goals>
<goal>schemagen</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Renaming default 'schema1.xsd' -->
<transformSchemas>
<transformSchema>
<uri>http://www.your.url/namespace/foo</uri>
<toFile>your-schema-name.xsd</toFile>
</transformSchema>
</transformSchemas>
</configuration>
</plugin>
...
<plugins>
<build>
(2) Add a package info (optional) class:
(2)添加一个包信息(可选)类:
package-info.java
to your (java) package(s). This file contains the package name:
package-info.java
到您的(java)包。此文件包含包名称:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.your.url/namespace/foo", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package url.your.namespace.foo;
(3) Add XML annotations to your classes like
(3) 将 XML 注释添加到您的类中,例如
@XmlRootElement(name = "Container")
@XmlAccessorType(XmlAccessType.FIELD)
public class Container {
@XmlElement(name = "Info", required = true)
private Info info;
@XmlElement(name = "Unit")
private Unit unit;
...}
Then you just have to execute your maven build and then in the target folder you'll find you xsd file.
然后你只需要执行你的 maven 构建,然后在目标文件夹中你会找到你的 xsd 文件。