Pojo 到 xsd 生成

发布于 2024-08-02 17:08:22 字数 70 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剪不断理还乱 2024-08-09 17:08:22

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

您可以在 AMIS 博客JavaPassion 站点

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

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

如何视而不见 2024-08-09 17:08:22

JiBX 执行此操作

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

JiBX does this

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.

不可一世的女人 2024-08-09 17:08:22

我会这样做:

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

如何使用上面的方法

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

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);
    }
愚人国度 2024-08-09 17:08:22

感谢您提供方法。只是想添加完整的示例。

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

    }

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

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文