我需要用JAXB生成如下的XML文件
但是無法加入圖中的屬性type。
我的代碼如下:
package Task2.Generated;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"food"
})
@XmlRootElement(name = "menu")
public class Menu {
@XmlElement(required = true)
protected List<Menu.Food> food;
public List<Menu.Food> getFood() {
if (food == null) {
food = new ArrayList<Menu.Food>();
}
return this.food;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"price",
"description",
"calories",
//"type"
})
public static class Food {
//private String type;
@XmlElement(required = true)
protected String name;
@XmlElement(defaultValue = "1")
protected float price;
@XmlElement(required = true, defaultValue = "")
protected String description;
protected int calories;
@XmlAttribute(name = "category", required = true)
protected String category;
@XmlAttribute(name = "type", required = true)
protected String type;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public float getPrice() {
return price;
}
public void setPrice(float value) {
this.price = value;
}
public String getDescription() {
return description;
}
public void setDescription(String value) {
this.description = value;
}
public int getCalories() {
return calories;
}
public void setCalories(int value) {
this.calories = value;
}
public String getCategory() {
return category;
}
public void setCategory(String value) {
this.category = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"type"
})
public static class Description {
//private String type;
@XmlAttribute(name = "type", required = true)
protected String type;
public String getType() {
return type;
}
public void setType(String value) {
this.type = value;
}
}
}
}
自己做出了了,就是加個class