Reputation: 201
I need some framework to create xsd from java object.
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.
for example:
My java class:
public class Example {
public List<String> elements;
}
Insert value Yo the Object:
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:
<?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>
Upvotes: 1
Views: 542
Reputation: 666
What you need is not XJC, but a different JAXB tool namely schemagen. It's usage is pretty straightforward and clearly explained here.
And as an example, I tried the following:
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
<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
<?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>
Upvotes: 1
Reputation: 29673
You can do it with Maven tool using jaxb2:xjc plugin
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>
Plugin usage
Upvotes: 0