utility to generate xsd from java class

∥☆過路亽.° 提交于 2019-12-22 04:44:17

问题


I want to generate xsd for the following class

public class Node{
  private String value;
  private List<Node> childrens;

}

What is the best utility to generate xsd schema for such code

In general I want to implement simple tree. I'm already using jaxb for generating the classes from schema.


回答1:


If you're already using JAXB, you can use the schemagen tool for creating an 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

There are also Ant tasks and Maven plugins for doing the same in an automated fashion.




回答2:


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

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);
            }

        });

    }

}



回答3:


There are also Ant tasks and Maven plugins for doing the same in an automated fashion.

Yes indeed there are. Before you have to figure it out for yourself here is the maven version:

(1) Add the maven plugin to your pom.xml

<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:

package-info.java to your (java) package(s). 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

@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.



来源:https://stackoverflow.com/questions/9681373/utility-to-generate-xsd-from-java-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!