gpt4 book ai didi

java - 在不使用文件的情况下从 JAXB 注释类生成 XSD

转载 作者:数据小太阳 更新时间:2023-10-29 02:21:56 25 4
gpt4 key购买 nike

我正在尝试按照本文中提到的代码从 Java 注释类生成 XSD 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 获取为字符串。

SchemaOutputResolver 的实现是否有可能不将文件写入磁盘并使用字符串值返回或设置一些实例变量。

最佳答案

您可以在 StringWriter 上编写 StreamResult 并从中获取字符串。

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

}

关于java - 在不使用文件的情况下从 JAXB 注释类生成 XSD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24038802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com