JAXB 和属性排序

发布于 2024-10-27 13:40:46 字数 2206 浏览 2 评论 0原文

我希望 Java 类的序列化 XML 输出遵循 Java 类中属性的顺序。

JAXB 似乎按字母顺序排序。

我可以通过使用 @XmlTypepropOrder 并指定所有属性来覆盖它,但我有一个包含许多属性的类,并且这些属性尚未最终确定。

我读到指定一个空的 propOrder 可以做到这一点,但事实并非如此。

我的示例类:

package test;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
//@XmlType(propOrder={"company", "scheme", "agreementNumber"})
@XmlType(propOrder={}) // makes no difference - still alphabetical in XML 
public class CustomerPlan2 {

    private String company;
    private String scheme;
    private String agreementNumber;

    @XmlElement(name="Company")
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }

    @XmlElement(name="Scheme")
    public String getScheme() {
        return scheme;
    }
    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    @XmlElement(name="AgreementNumber")
    public String getAgreementNumber() {
        return agreementNumber;
    }
    public void setAgreementNumber(String agreementNumber) {
        this.agreementNumber = agreementNumber;
    }
}

我的序列化代码:

    CustomerPlan2 cp2 = new CustomerPlan2();

    cp2.setCompany("company");
    cp2.setScheme("scheme");
    cp2.setAgreementNumber("agreementnumber");
    JAXBContext context = JAXBContext.newInstance(CustomerPlan2.class);
    Marshaller marshaller = context.createMarshaller();

    marshaller.marshal(cp2, new FileWriter("C:\\temp\\out.xml"));

输出:

    <customerPlan2>
      <AgreementNumber>agreementnumber</AgreementNumber> 
      <Company>company</Company> 
      <Scheme>scheme</Scheme> 
    </customerPlan2>

我希望我的输出是(作为我的类的属性顺序):

    <customerPlan2>
      <Company>company</Company>
      <Scheme>scheme</Scheme> 
      <AgreementNumber>agreementnumber</AgreementNumber> 
    </customerPlan2>

感谢您对此的任何帮助。

I want the serialized XML output from my Java class to honor the ordering of the properties in the Java class.

It seems that JAXB orders alphabetically.

I can override this by using @XmlType with propOrder and specifying ALL of the properties, but I have a class with many properties and these are not yet finalized.

I read that specifying an empty propOrder would do it but it don't.

My example class:

package test;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
//@XmlType(propOrder={"company", "scheme", "agreementNumber"})
@XmlType(propOrder={}) // makes no difference - still alphabetical in XML 
public class CustomerPlan2 {

    private String company;
    private String scheme;
    private String agreementNumber;

    @XmlElement(name="Company")
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }

    @XmlElement(name="Scheme")
    public String getScheme() {
        return scheme;
    }
    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    @XmlElement(name="AgreementNumber")
    public String getAgreementNumber() {
        return agreementNumber;
    }
    public void setAgreementNumber(String agreementNumber) {
        this.agreementNumber = agreementNumber;
    }
}

My serialize code:

    CustomerPlan2 cp2 = new CustomerPlan2();

    cp2.setCompany("company");
    cp2.setScheme("scheme");
    cp2.setAgreementNumber("agreementnumber");
    JAXBContext context = JAXBContext.newInstance(CustomerPlan2.class);
    Marshaller marshaller = context.createMarshaller();

    marshaller.marshal(cp2, new FileWriter("C:\\temp\\out.xml"));

Output:

    <customerPlan2>
      <AgreementNumber>agreementnumber</AgreementNumber> 
      <Company>company</Company> 
      <Scheme>scheme</Scheme> 
    </customerPlan2>

I want my output to be (as the property order of my class):

    <customerPlan2>
      <Company>company</Company>
      <Scheme>scheme</Scheme> 
      <AgreementNumber>agreementnumber</AgreementNumber> 
    </customerPlan2>

Thanks for any help on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

⒈起吃苦の倖褔 2024-11-03 13:40:46

可以使用:

@XmlType (propOrder={"prop1","prop2",..."propN"})

只需取消注释代码,如下所示:

//@XmlType(propOrder={"company", "scheme", "agreementNumber"})

这是正确的用法。

It's possible using:

@XmlType (propOrder={"prop1","prop2",..."propN"})

Just uncomment the code like this:

//@XmlType(propOrder={"company", "scheme", "agreementNumber"})

This is the correct usage.

清君侧 2024-11-03 13:40:46

注:我引导 EclipseLink JAXB (MOXy)

Java 反射返回字段列表的顺序/property 不得到保证。这就是 JAXB 实现不使用它来确定元素顺序的原因。

默认情况下,JAXB 提供 不保证订购。然而,大多数(如果不是所有 JAXB 实现)都使用字母顺序,因为它是确定性的。为了保证这个顺序,你必须按如下方式注释你的类:

@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class Foo {
    ...
}

Note: I lead EclipseLink JAXB (MOXy)

The order in which Java reflection returns the list of fields/properties is not guaranteed. This is why JAXB implementations do not use it to determine element order.

By default JAXB provides no guaranteed ordering. However most (if not all JAXB implementations) use alphabetical ordering since it is deterministic. To guarantee this ordering you must annotate your class as follows:

@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class Foo {
    ...
}
拧巴小姐 2024-11-03 13:40:46
@XmlType(propOrder={"company", "scheme", "agreementNumber"})

这是正确的,但是你尝试过吗?

@XmlType(propOrder={"Company", "Scheme", "AgreementNumber"})
@XmlType(propOrder={"company", "scheme", "agreementNumber"})

It's correct, but have you tried this?

@XmlType(propOrder={"Company", "Scheme", "AgreementNumber"})
—━☆沉默づ 2024-11-03 13:40:46

这个线程很旧,但值得抛出我如何让​​我的属性以正确的顺序生成 xml,而不是使用字母顺序,因为这是不希望的。需要注意的一件事是,我使用的是 wink 和 jaxb,它们的行为可能与其他提供程序不同。 Wink 对于正确列表中的内容非常具体。即使是我标记为 xml 瞬态的元素,或者根本没有修饰的元素也需要成为其中的一部分

@XmlRootElement(name = "Product")
@XmlType(name="",propOrder={"productName","productVersion",..."propN"})

......不可否认,我不太了解它为什么有效!:)

This thread is old but worth throwing how I got my properties to generate xml in the proper order and NOT using alphabetical ordering as that was undesired. One thing to note is that I am using wink and jaxb which may behave differently then other providers. Wink was very specific about what was in the propertly list. Even elements I mark as xml transient, or not decorated at all were required to be part of

@XmlRootElement(name = "Product")
@XmlType(name="",propOrder={"productName","productVersion",..."propN"})

...admittedly I don't enough of WHY it works!:)

或十年 2024-11-03 13:40:46

根据 this,不保证同级 XML 元素的顺序。

According to this, the order of sibling XML elements is not guaranteed.

长亭外,古道边 2024-11-03 13:40:46

只需添加:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"field1", "field2", ...})

Just add :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"field1", "field2", ...})
秋叶绚丽 2024-11-03 13:40:46

@XmlType(propOrder={"prop1", "prop2"}) 中,您只能声明您在类中声明的 propertyName。您不能

XMLElement name (
@XmlElement(name="Company"))

XmlType propOrder 中声明,如上所述。

In @XmlType(propOrder={"prop1", "prop2"}) you can declare only propertyName you declared in the class. You cannot declare

XMLElement name (
@XmlElement(name="Company"))

in the XmlType propOrder as mentioned by above..

淡水深流 2024-11-03 13:40:46

这是正确的:请取消注释下面的行

@XmlType(propOrder={"company", "scheme", "agreementNumber"})

This is correct : Please uncomment below line

@XmlType(propOrder={"company", "scheme", "agreementNumber"})
ゞ记忆︶ㄣ 2024-11-03 13:40:46

您必须向该类添加 propOrderXmlAccessType 注释。

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(propOrder = {"PartyType","PartyName","PartyAddress"})

You have to add the propOrder and the XmlAccessType annotations to the class.

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(propOrder = {"PartyType","PartyName","PartyAddress"})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文