Generate a XSD from a JAXB-annotated class without using File(在不使用 File 的情况下从带有 JAXB 注释的类生成 XSD)
问题描述
我正在尝试按照本文中提到的代码从 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在不使用 File 的情况下从带有 JAXB 注释的类生成
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01