JAXB 和属性排序
我希望 Java 类的序列化 XML 输出遵循 Java 类中属性的顺序。
JAXB 似乎按字母顺序排序。
我可以通过使用 @XmlType
和 propOrder 并指定所有属性来覆盖它,但我有一个包含许多属性的类,并且这些属性尚未最终确定。
我读到指定一个空的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
可以使用:
只需取消注释代码,如下所示:
这是正确的用法。
It's possible using:
Just uncomment the code like this:
This is the correct usage.
注:我引导 EclipseLink JAXB (MOXy)
Java 反射返回字段列表的顺序/property 不得到保证。这就是 JAXB 实现不使用它来确定元素顺序的原因。
默认情况下,JAXB 提供 不保证订购。然而,大多数(如果不是所有 JAXB 实现)都使用字母顺序,因为它是确定性的。为了保证这个顺序,你必须按如下方式注释你的类:
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:
这是正确的,但是你尝试过吗?
It's correct, but have you tried this?
这个线程很旧,但值得抛出我如何让我的属性以正确的顺序生成 xml,而不是使用字母顺序,因为这是不希望的。需要注意的一件事是,我使用的是 wink 和 jaxb,它们的行为可能与其他提供程序不同。 Wink 对于正确列表中的内容非常具体。即使是我标记为 xml 瞬态的元素,或者根本没有修饰的元素也需要成为其中的一部分
......不可否认,我不太了解它为什么有效!:)
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
...admittedly I don't enough of WHY it works!:)
根据 this,不保证同级 XML 元素的顺序。
According to this, the order of sibling XML elements is not guaranteed.
只需添加:
Just add :
在
@XmlType(propOrder={"prop1", "prop2"})
中,您只能声明您在类中声明的propertyName
。您不能在
XmlType
propOrder
中声明,如上所述。In
@XmlType(propOrder={"prop1", "prop2"})
you can declare onlypropertyName
you declared in the class. You cannot declarein the
XmlType
propOrder
as mentioned by above..这是正确的:请取消注释下面的行
This is correct : Please uncomment below line
您必须向该类添加
propOrder
和XmlAccessType
注释。You have to add the
propOrder
and theXmlAccessType
annotations to the class.