浅水微渤 2016-01-22 21:39 采纳率: 100%
浏览 2101
已采纳

关于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;
        }
  }
}

}

  • 写回答

4条回答 默认 最新

  • 浅水微渤 2016-01-28 00:16
    关注

    自己做出了了,就是加个class

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?