Pojo to xsd generation

2020-03-13 05:40发布

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 xml xsd pojo
4条回答
Bombasti
2楼-- · 2020-03-13 06:17

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.

查看更多
男人必须洒脱
3楼-- · 2020-03-13 06:19

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.

查看更多
够拽才男人
4楼-- · 2020-03-13 06:22

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

    }
查看更多
冷血范
5楼-- · 2020-03-13 06:28

Here is how I would do it:

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);
    Document doc = (Document) 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);
}
查看更多
登录 后发表回答