2012-01-03 48 views
5

我想为我的项目生成一些XML模式。我有一些Java类,像这样的:从Java类生成XML模式(或相反)

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() { 
    } 
} 

我想基于这些类为我的项目XML模式,让其他开发者可以基于建立一个XML文件生成模式,他们可以在发送给我之前验证信息。但是我不知道什么是最好的,是基于一个类来生成一个XML Schema,还是基于一个XML Schema生成一个类。请问,你能指出有哪些工具可以做到这一点,以及使用这些方法中的每一种的好处吗?

我知道有一个名为JAXB的工具,但我不知道它是否同时执行这些工作或其中的任何一个。

回答

2

在JDK bin目录内有一个名为schemagen的工具,它将java源/类文件转换为XML模式。请参阅documentation

3

是的,JAXB可以双向使用(使用注释)。查看this question了解更多信息和一些链接。