Generate XSD from XML programmatically in Java

风流意气都作罢 提交于 2020-01-04 06:26:24

问题


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?


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



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


来源:https://stackoverflow.com/questions/27314066/generate-xsd-from-xml-programmatically-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!