首页 \ 问答 \ JAXB和带有元素列表的属性排序(JAXB and property ordering with list of elements)

JAXB和带有元素列表的属性排序(JAXB and property ordering with list of elements)

我正在使用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中定义了一个属性。 但由于属性是多个,可能是一个或多个,我不知道实现它。 或者你知道其他一些订购元素的方法

感谢您的帮助


I am using JAXB to unmarshall xml file. here is my code for the element feature, but I wanna have a special order of the elements in the element feature, like this

<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>

I checked some tutorial of @XmlType(propOrder = {}), but I cannot find a way to order with list of elements like the attribute elements here.

here is my codes

@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;
    }
}

It always throw out exception, since I only define one attribute in propOrder. But since the attribute is multiple, could be one or more, I do not have any idea to implement it. Or do you know some other way to order the elements

Thanks for your help in advance


原文:https://stackoverflow.com/questions/20708049
更新时间:2022-02-05 08:02

最满意答案

规格说UNIQUE

HTML 4.01规范说ID必须是文档范围内唯一的。

HTML 5规范说同样的事情,换句话说。 它说ID必须在其主子树中是唯一的,如果我们读取它的定义,它基本上就是文档

避免重复

但是,由于HTML呈现器在HTML呈现方面非常宽容,因此它们允许重复ID。 如果可能的话应该避免这种情况,并且在以编程方式通过JavaScript中的ID访问元素时应该严格避免 。 当找到几个匹配的元素时,我不确定getElementById函数应该返回什么? 应该是:

  • 返回错误?
  • 返回第一个匹配元素?
  • 返回最后匹配元素?
  • 返回一组匹配元素?
  • 没有回报?

但即使浏览器现在可靠地工作,也没有人能够在将来保证这种行为,因为这违反了规范。 这就是为什么我建议你永远不要在同一个文档中复制ID。

这是由danludwig提出的软件工程部Robert Koritnik的回答
问题: 两个具有相同id属性的HTML元素:它真的有多糟糕?

HTML中不允许重复的ID

那段代码不正确。 不正确的不是灰色阴影。 此代码违反了标准,因此不正确。 它将无法通过验证检查,它应该。 也就是说,目前市场上没有任何浏览器会抱怨它,或者根本没有任何问题。 浏览器会在他们的权利范围内抱怨它,但目前任何一个版本都没有。 这并不意味着未来版本可能不会严重对待此代码。

〜来自丹雷

重复的ID和JavaScript

因此,如果您在HTML中使用重复的ID,则许多库将无法按预期工作。 大多数库将获得他们找到的第一个id并返回该元素。 当我们看看纯JavaScript时: document.getElementById("idName"); 应该返回多个具有相同id的元素。 它说它必须以树的顺序返回第一个元素。


Specification says UNIQUE

HTML 4.01 specification says ID must be document-wide unique.

HTML 5 specification says the same thing but in other words. It says that ID must be unique in its home subtree, which is basically the document if we read the definition of it.

Avoid duplication

But since HTML renderers are very forgiving when it comes to HTML rendering they permit duplicate IDs. This should be avoided if at all possible and strictly avoided when programmatically accessing elements by IDs in JavaScript. I'm not sure what getElementById function should return when several matching elements are found? Should it:

  • return an error?
  • return first matching element?
  • return last matching element?
  • return a set of matching elements?
  • return nothing?

But even if browsers work reliably these days, nobody can guarantee this behavior in the future since this is against specification. That's why I recommend you never duplicate IDs within the same document.

This is an answer by Robert Koritnik at Software Engineering asked by danludwig
Question: Two HTML elements with same id attribute: How bad is it really?

Duplicate ids not allowed in HTML

That code is incorrect. Incorrect doesn't come in shades of grey. This code violates the standard and is therefore incorrect. It would fail validation checking, and it should. That said, no browser currently on the market would complain about it, or have any problem with it at all. Browsers would be within their rights o complain about it, but none of the current versions of any of them currently do. Which doesn't mean future versions might not treat this code badly.

~From Dan Ray

Duplicate ids and JavaScript

So if you use duplicate ids in your HTML many libraries will not work as expected. The most libraries will get the first id they find and return that element. When we look at pure JavaScript: the document.getElementById("idName"); should return in the case of multiple elements with the same id. It says it must return the first element, in tree order.

相关问答

更多
  • 规格说UNIQUE HTML 4.01规范说ID必须是文档范围内唯一的。 HTML 5规范说同样的事情,换句话说。 它说ID必须在其主子树中是唯一的,如果我们读取它的定义,它基本上就是文档。 避免重复 但是,由于HTML呈现器在HTML呈现方面非常宽容,因此它们允许重复ID。 如果可能的话应该避免这种情况,并且在以编程方式通过JavaScript中的ID访问元素时应该严格避免 。 当找到几个匹配的元素时,我不确定getElementById函数应该返回什么? 应该是: 返回错误? 返回第一个匹配元素? 返回 ...
  • 主要原因可能是: 浪费空间来维护没有意义的重复记录。 在应用程序中选择数据时,会不必要地浪费内存空间(主内存)。 规范化约束:如果您的表有重复记录,那么它根本就不是规范化表。 如果一个人存在2条记录,那么它真的很混乱,因为可能会有一个客户具有相同的名称,地址等。例如:银行客户。 因此,如果您没有定义明确识别此人的方法,那么您将如何说帐户X属于哪个人。 The main reason could be: Wastage of space for maintaining duplicate records wh ...
  • 实际上HTML和XHTML有区别。 由于XHTML是XML,适用于XML标识的规则: 类型ID的值必须与“名称”生产相匹配。 NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | ...
  • 基于文档: AMP HTML标签附录 AMP HTML允许大多数HTML5标准标签和一些特定于AMP运行时的附加标签。 AMP Spec广泛定义了不允许的标记集。 但是,AMP Validator实现必须使用标签白名单来实现。 本附录列出了AMP验证器应该列入白名单的标签集。 如果HTML标记不在此列表中,则AMP验证程序不会认为该标记在任何上下文中都有效。 但是,许多这些标签都有其他限制。 例如,