3

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?

2 Answers 2

16

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));
4
  • Perfect timing - just looking for this :-)
    – Gary
    May 8, 2015 at 10:41
  • Very helpful indeed! Mar 1, 2017 at 11:40
  • To use this, we also need to add xom jar because it contain nu.xom.parsingException class, if you don't use then it will throw error.
    – yash
    May 30, 2018 at 9:06
  • Small warning - I have found that this does not find all child elements for large xml files. Jan 31, 2020 at 7:45
3

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")

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.