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.
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.
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.
JAXB can do both
There's a tool called schemagen
inside the JDK bin
directory, that turns java source/class file into an XML schema. See the documentation.
Yes, JAXB can go both ways (using annotations). Check out this question for more info and some links.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With