程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> java-關於JAXB生成XML的小問題

java-關於JAXB生成XML的小問題

編輯:編程解疑
關於JAXB生成XML的小問題

我需要用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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved