Generate XSD from XML programmatically in Java

10,480

Solution 1

I am the author of the tool xsd-gen. I converted the tool to be a library as well, and uploaded the artifact to Maven Central:

<dependency>
    <groupId>org.wiztools</groupId>
    <artifactId>xsd-gen</artifactId>
    <version>0.2.1</version>
</dependency>

Now it is simple to use as a library within your application:

import org.wiztools.xsdgen.XsdGen;
import java.io.File;
import java.io.FileOutputStream;

...

XsdGen gen = new XsdGen();
gen.parse(new File("in.xml"));
File out = new File("out.xsd");
gen.write(new FileOutputStream(out));

Solution 2

I included the xsd-gen source code and it worked for me. You only need

  1. TypeInferenceUtil.java
  2. XsdGen.java

The package declarations I used (for Gradle) were:

compile("com.io7m.xom:xom:1.2.10")
compile("org.wiztools.commons:wiztools-commons-lib:0.4.1")
Share:
10,480
Jack Gibson
Author by

Jack Gibson

Updated on June 24, 2022

Comments

  • Jack Gibson
    Jack Gibson almost 2 years

    I'm looking for a leightweight library that allows me to genereate an XSD from XML in Java (no commandline tool). I know that it is not a clean way to generate it, but in this case I need to do it. Also the XML is very simple in terms of structure.

    I already looked into Trang, but there is no API documentation except how to call it from command line.

    Also I checked out xsd-gen, but the issue with that library is that one would need to modify some package declrations in the source code which I couldn't find.

    Any other suggestions?