java Pojo 到 xsd 生成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1325048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:08:16  来源:igfitidea点击:

Pojo to xsd generation

javaxmlxsdpojo

提问by Surya

Is there a library which could generate a xsd schema from a java class? Google yields lots of results the opposite ( java classes from xsd ).

是否有可以从 java 类生成 xsd 模式的库?谷歌产生了很多相反的结果(来自 xsd 的 java 类)。

采纳答案by Vineet Reynolds

JAXB 2.0 allows you to create a XML schema from an annotated Java class.

JAXB 2.0 允许您从带注释的 Java 类创建 XML 模式。

You'll find some examples at the AMIS blogand at the JavaPassion site.

您可以在AMIS 博客JavaPassion 站点中找到一些示例。

回答by ra9r

Here is how I would do it:

这是我将如何做到的:

public static void pojoToXSD(Class<?> pojo, OutputStream out) throws IOException, TransformerException, JAXBException {
    JAXBContext context = JAXBContext.newInstance(pojo);
    final List<DOMResult> results = new ArrayList<>();

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

    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(domResult.getNode());
    StreamResult result = new StreamResult(out);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(source, result);
}

How to use the method above

如何使用上述方法

try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        pojoToXSD(NESingleResponse.class, stream);

        String finalString = new String(stream.toByteArray());
        System.out.println(finalString);
    } catch (JAXBException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }

回答by skaffman

JiBXdoes this

JiBX这样做

The schema generator tool first reads one or more JiBX binding definitions and then uses reflection to interpret the structure of the Java classes referenced in the bindings. By combining the binding definitions with the actual class information the schema generator is able to construct one or more XML schemas to represent the documents handled by the bindings.

模式生成器工具首先读取一个或多个 JiBX 绑定定义,然后使用反射来解释绑定中引用的 Java 类的结构。通过将绑定定义与实际类信息相结合,模式生成器能够构建一个或多个 XML 模式来表示由绑定处理的文档。

回答by Rajaram

Thanks for giving your method. Just wanted to add complete example.

谢谢你提供你的方法。只是想添加完整的例子。

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

    }