简体   繁体   中英

Generate XSD from XML programmatically in Java

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM