从使用JAXB注释的类生成XSD,而无需使用File [英] Generate a XSD from a JAXB-annotated class without using File

查看:73
本文介绍了从使用JAXB注释的类生成XSD,而无需使用File的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照本文此技术正在使用文件系统,我的要求是不使用文件系统就将XML作为字符串获取.

This technique is using File system, My requirement is to get the XML as String without using file system.

SchemaOutputResolver的实现是否有可能不会将文件写入磁盘并返回或使用String值设置某些实例变量.

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

}

这篇关于从使用JAXB注释的类生成XSD,而无需使用File的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆