Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

utility to generate xsd from java class

Tags:

java

xsd

I want to generate xsd for the following class

public class Node{
  private String value;
  private List<Node> childrens;

}

What is the best utility to generate xsd schema for such code

In general I want to implement simple tree. I'm already using jaxb for generating the classes from schema.

like image 798
Julias Avatar asked Mar 13 '12 09:03

Julias


People also ask

How do I create an XSD file?

With the desired XML document opened in the active editor tab, choose Tools | XML Actions | Generate XSD Schema from XML File on the main menu. The Generate Schema From Instance Document dialog box opens. and select the desired file in the dialog that opens.

How do I create an XSD class in Intellij?

In the active editor tab, open the desired Schema . xsd file or an XML document, which contains the desired Schema. In the main menu, go to Tools | XML Actions | Generate Java Code From XML Schema Using XmlBeans.

Which command line command is used to generate XML schemas from Java source files?

Use the JAXB schema compiler, xjc command to generate JAXB-annotated Java classes. The schema compiler is located in the app_server_root \bin\ directory. The schema compiler produces a set of packages containing Java source files and JAXB property files depending on the binding options used for compilation.


1 Answers

You can use the generateSchema API on JAXBContext to generate an XML schema:

import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Node.class);   
        jc.generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(String namespaceURI, String suggestedFileName)
                throws IOException {
                return new StreamResult(suggestedFileName);
            }

        });

    }

}
like image 69
bdoughan Avatar answered Oct 17 '22 23:10

bdoughan