简体   繁体   English

从java类生成xsd的实用程序

[英]utility to generate xsd from java class

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 从模式生成类。

You can use the generateSchema API on JAXBContext to generate an XML schema:您可以使用JAXBContext上的generateSchema API 来生成 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);
            }

        });

    }

}

If you're already using JAXB, you can use the schemagen tool for creating an XSD:如果您已经在使用 JAXB,则可以使用schemagen工具来创建 XSD:

There are also Ant tasks and Maven plugins for doing the same in an automated fashion.还有 Ant 任务和 Maven 插件可以以自动化方式执行相同的操作。

If you are using Eclipse IDE, you can follow the steps from this official documentation:如果您使用的是 Eclipse IDE,则可以按照此官方文档中的步骤操作:

Generating Schema from Classes 从类生成模式

  1. From the Navigator or Project Explorer, select File > New > Other.从导航器或项目资源管理器中,选择文件 > 新建 > 其他。 The Select a wizard dialog appears.出现选择向导对话框。
  2. Select JAXB > Schema from JAXB Classes and then click Next.从 JAXB 类中选择 JAXB > Schema,然后单击下一步。 The Generate Schema from Classes page of the Generate Schema from JAXB Classes Wizard appears.将出现从 JAXB 类生成架构向导的从类生成架构页面。
  3. Select the project, package, or classes from which to generate the schema and click Finish.选择要从中生成模式的项目、包或类,然后单击完成。

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). package-info.java到您的(java)包。 This file contains the package name:此文件包含包名称:

@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 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM