4

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
SHiRKiT
  • 1,024
  • 3
  • 11
  • 30

3 Answers3

8

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
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • Thank you. I've created an AntTask within my netbeans that generates the schema for my project, just for the interesting classes. It's perfect for my solution, spent about 2 hours to make this completly working. – SHiRKiT Jan 04 '12 at 16:23
4

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
3

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

Community
  • 1
  • 1
sarumont
  • 1,734
  • 12
  • 11