Generate XML Schema from Java class (or the opposi

2020-06-04 07:56发布

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.

标签: java xsd
3条回答
相关推荐>>
2楼-- · 2020-06-04 08:09

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

查看更多
啃猪蹄的小仙女
4楼-- · 2020-06-04 08:27

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

查看更多
登录 后发表回答