問題描述
我正在嘗試按照本文中提到的代碼從 Java Annotated 類生成 XSD 是否可以從帶有 JAXB 注釋的類生成 XSD
I am trying to generate XSD from Java Annotated classes by following code mentioned in this post Is it possible to generate a XSD from a JAXB-annotated class
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
SchemaOutputResolver sor = new MySchemaOutputResolver();
jaxbContext.generateSchema(sor);
public class MySchemaOutputResolver extends SchemaOutputResolver {
public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
File file = new File(suggestedFileName);
StreamResult result = new StreamResult(file);
result.setSystemId(file.toURI().toURL().toString());
return result;
}
}
此技術使用文件系統,我的要求是在不使用文件系統的情況下將 XML 作為字符串.
This technique is using File system, My requirement is to get the XML as String without using file system.
SchemaOutputResolver
的實現是否有可能不會將文件寫入磁盤并返回或設置一些具有字符串值的實例變量.
Is there any possibility the Implementation of SchemaOutputResolver
may not write file to disk and return or set some instance variable with the String value.
推薦答案
您可以在 StringWriter
上編寫 StreamResult
并從中獲取字符串.
You can write the StreamResult
on a StringWriter
and get the string from that.
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
MySchemaOutputResolver sor = new MySchemaOutputResolver();
jaxbContext.generateSchema(sor);
String schema = sor.getSchema();
public class MySchemaOutputResolver extends SchemaOutputResolver {
private StringWriter stringWriter = new StringWriter();
public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
StreamResult result = new StreamResult(stringWriter);
result.setSystemId(suggestedFileName);
return result;
}
public String getSchema() {
return stringWriter.toString();
}
}
這篇關于在不使用 File 的情況下從帶有 JAXB 注釋的類生成 XSD的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!