【问题标题】:JAXB and property ordering with list of elementsJAXB 和带有元素列表的属性排序
【发布时间】:2013-12-20 16:35:09
【问题描述】:

我正在使用 JAXB 来解组 xml 文件。 这是我的元素功能代码,但我想要元素功能中元素的特殊顺序,像这样

<feature>
  <name>2D Polynomial Approximation of Log of ConstantQ</name>
  <active>false</active>
  <attribute>50</attribute>
  <attribute>20</attribute>
  <attribute>10</attribute>
  <attribute>10</attribute>
</feature>

我查看了@XmlType(propOrder = {}) 的一些教程,但我找不到使用此处的属性元素等元素列表进行排序的方法。

这是我的代码

@XmlRootElement(name = "feature")
@XmlType(propOrder = {"name", "active","attribute"})
public class Feature{

    String name;

    boolean active;

    List<String> attributes = new LinkedList<String>();

    /**
     * name element of feature element
     * @return
     */
    @XmlElement(name = "name")
    public final String getName(){
        return this.name;
    }

    public final void setName(String name){
        this.name = name;
    }

    /**
     * active element
     * @return
     */
    @XmlElement(name = "active")
    public final boolean getActive(){
        return this.active;
    }

    public final void setActive(boolean active){
        this.active = active;
    }

    /**
     * attribute elements
     * @return
     */
    @XmlElement(name = "attribute")
    public final List<String> getAttributes(){
        return this.attributes;
    }

    public final void setAttributes(List<String> attributes){
        this.attributes = attributes;
    }
}

它总是抛出异常,因为我只在 propOrder 中定义了一个属性。但是由于属性是多个,可能是一个或多个,我没有任何想法来实现它。 或者你知道其他排序元素的方法

提前感谢您的帮助

【问题讨论】:

    标签: java xml jaxb


    【解决方案1】:

    propOrder 基于字段/属性名称,而不是 XML 元素名称。所以你应该有

    @XmlType(propOrder = {"name", "active","attributes"})
    

    更多信息

    【讨论】:

    • 嘿,Blaise,Thnks,你能详细解释一下如何实现它吗?
    • @ZYJ - 我已经添加了您需要将 propOrder 更改为的内容。
    • 啊,好的,我明白了,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2012-06-06
    • 2011-05-10
    相关资源
    最近更新 更多