浅水微渤 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条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据