是否有可以从java类生成xsd架构的库? 谷歌产生了相反的结果(来自xsd的java类)。
答案 0 :(得分:8)
JAXB 2.0允许您从带注释的Java类创建XML模式。
您可以在AMIS blog和JavaPassion site找到一些示例。
答案 1 :(得分:3)
JiBX执行此操作
架构生成器工具首先读取 一个或多个JiBX绑定定义 然后使用反射来解释 Java类的结构 在绑定中引用。通过 将绑定定义与 实际的班级信息 模式生成器能够构造 要表示的一个或多个XML模式 绑定处理的文件。
答案 2 :(得分:3)
我将如何做到这一点:
public void pojoToXSD(JAXBContext context, Object pojo, OutputStream out)
throws IOException, TransformerException
{
final List<DOMResult> results = new ArrayList<DOMResult>();
context.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String ns, String file)
throws IOException {
DOMResult result = new DOMResult();
result.setSystemId(file);
results.add(result);
return result;
}
});
DOMResult domResult = results.get(0);
Document doc = (Document) domResult.getNode();
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
}
答案 3 :(得分:-1)
感谢您提供方法。只是想添加完整的例子。
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import test.Test;
public class Main {
public static void main(String[] args) throws JAXBException,
FileNotFoundException {
JAXBContext context = JAXBContext.newInstance("test");
try {
new Main().pojoToXSD(context, new Test(), System.out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void pojoToXSD(JAXBContext context, Object pojo, OutputStream out)
throws IOException, TransformerException
{
final List<DOMResult> results = new ArrayList<DOMResult>();
context.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String ns, String file)
throws IOException {
DOMResult result = new DOMResult();
result.setSystemId(file);
results.add(result);
return result;
}
});
DOMResult domResult = results.get(0);
com.sun.org.apache.xerces.internal.dom.DocumentImpl doc = com.sun.org.apache.xerces.internal.dom.DocumentImpl) domResult.getNode();
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
}
}
//---------- below will go in test package
package test;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private final static QName _Test_QNAME = new Name("urn:vertexinc:enterprise:calendar:1:0", "Test");
public ObjectFactory() {
}
public Test createTest() {
return new Test();
}
}
package test;
public class Test {
String name;
String cls;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
}