1

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>

1
  • If you found one of the answers helpful, please consider marking them accordingly. Or comment on why not. Commented Sep 20, 2013 at 11:06

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Downvoted because this answer deals with generating Java files for an existing XSD while the (admittedly confusing) question is on how to generate XSD for existing Java files. Schemagen turned out to be the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.