我需要一些框架来创建从java对象的xsd。
我知道jaxb和xstream,但这些框架不是我需要的,因为这些框架是从java类XSD生成的,但我需要从java XSD实例的值生成。
例如:
我的java类:
public class Example {
public List<String> elements;
}向对象插入值Y:
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.
}
}这是我期望的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>
发布于 2013-09-12 23:46:51
您需要的不是XJC,而是一个不同的JAXB工具,即schemagen。它的用法非常简单,并且清楚地解释了here。
作为一个例子,我尝试了以下方法:
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;
}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>和output -> 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>发布于 2013-09-12 20:39:53
您可以通过使用的Maven工具来执行此操作
简单的例子
<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> 插件
https://stackoverflow.com/questions/18758435
复制相似问题