[英]framework to generate XSD from java object instance
I need some framework to create xsd from java object. 我需要一些框架来从java对象创建xsd。
i know jaxb and xstream but those frameworks is not what i need, because those framework generate from java class XSD, but i need to generate from values of instance of java XSD. 我知道jaxb和xstream,但是那些框架不是我所需要的,因为那些框架是从Java类XSD生成的,但是我需要从Java XSD实例的值生成。
for example: 例如:
My java class: 我的java课:
public class Example {
public List<String> elements;
}
Insert value Yo the Object: 插入值Yo对象:
public class Main {
public static void main(final String[] args) throws Exception {
Example e = new Example();
e.elements,add("a");
e.elements,add("b");
e.elements,add("c");
// Now i want to generate e.elements to xsd file like example below.
}
}
This is my expected xsd: 这是我期望的xsd:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="something">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="xs:string"/>
<xs:element name="b" type="xs:string"/>
<xs:element name="c" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
What you need is not XJC, but a different JAXB tool namely schemagen. 您所需的不是XJC,而是一个不同的JAXB工具,即schemagen。 It's usage is pretty straightforward and clearly explained here .
它的用法非常简单,并在此处明确说明。
And as an example, I tried the following: 作为示例,我尝试了以下操作:
Example.java Example.java
@XmlType(namespace = Namespaces.SOME_NAMESPACE,
propOrder = {"a", "b", "c"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Example {
@XmlElement(required = true, defaultValue = "requiredElementValue")
private String a;
@XmlAttribute(required = true)
private String b;
@XmlAttribute(required = false)
private String c;
}
Relevant portion of pom.xml pom.xml的相关部分
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>schemagen</id>
<goals>
<goal>schemagen</goal>
</goals>
</execution>
</executions>
<configuration>
<transformSchemas>
<transformSchema>
<uri>http://some/namespace</uri>
<toPrefix>some</toPrefix>
<toFile>myschema.xsd</toFile>
</transformSchema>
</transformSchemas>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
And the output -> myschema.xsd 然后输出-> myschema.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://some/namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="example">
<xs:sequence>
<xs:element name="a" type="xs:string" default="requiredElementValue"/>
</xs:sequence>
<xs:attribute name="b" type="xs:string" use="required"/>
<xs:attribute name="c" type="xs:string"/>
</xs:complexType>
</xs:schema>
You can do it with Maven tool using jaxb2:xjc plugin 您可以使用jaxb2:xjc插件使用Maven工具进行操作
Simple example 简单的例子
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>schema1-xjc</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaFiles>schema1.xsd</schemaFiles>
<packageName>com.example.foo</packageName>
<staleFile>${project.build.directory}/jaxb2/.schema1XjcStaleFlag</staleFile>
</configuration>
</execution>
</executions>
</plugin>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.