2012-01-03 15 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瞭解更多信息和一些鏈接。