Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate XML Schema from Java class (or the opposite)

Tags:

java

xsd

I wanted to generate some XML Schemas for my project. I have some Java Classes, like this one:

package com.fortresswars.entity;

import com.fortresswars.entity.properties.Armor;
import com.jme3.scene.Spatial;

public abstract class Object extends Thing {
    public Armor armor;
    public short hpMax;
    public boolean walkable = false;

    public short hpCurrent;
    public boolean alive;
    public Spatial object;

    public GameObject() {
        this.hpMax = hpMax;
        this.hpCurrent = hpMax;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
    }

    public void setHpCurrent(short hpCurrent) {
        this.hpCurrent = hpCurrent;
    }

    public void addHpCurrent(short hpToAdd) {
        this.hpCurrent += hpToAdd;
    }
}

package com.fortresswars.entity;

import com.jme3.texture.Image;
import eu.javaspecialists.tools.apt.NoArgsConstructor;

@NoArgsConstructor
public abstract class Thing {
    Image icon;

    public Thing() {
    }
}

I wanted to generate a XML Schema based on those classes for my project, so the other developers can build a XML file based on that generated Schema and they can validate the information before sending to me. But I don't know what is best, to generate a XML Schema based on a class, or generate a class based on a XML Schema. Please, could you point out what tools there is out there to do it, and the advantages of using each one of those approaches?

I know that there is a tool called JAXB, but I don't know if it does both of those jobs, or any of them.

like image 638
SHiRKiT Avatar asked Jan 03 '12 23:01

SHiRKiT


People also ask

How do you create a Java class from an XML Schema?

Generate and compile a Java class from an XML Schema using XMLBeans 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.


3 Answers

JAXB can do both

  1. Using JAXB schemagen tooling to generate an XML schema file from a Java class
  2. Using JAXB xjc tooling to generate JAXB classes from an XML schema file
like image 144
Aravind Yarram Avatar answered Oct 25 '22 20:10

Aravind Yarram


There's a tool called schemagen inside the JDK bin directory, that turns java source/class file into an XML schema. See the documentation.

like image 34
skaffman Avatar answered Oct 25 '22 20:10

skaffman


Yes, JAXB can go both ways (using annotations). Check out this question for more info and some links.

like image 3
sarumont Avatar answered Oct 25 '22 21:10

sarumont