为了可读性,我需要我的编组 xml 看起来像这样:
<tag-name name="name" attr1="value1" attr2="value2"/>
现在,当我整理它时,结果如下:
<tag-name attr2="value2" attr1="value1" name="name"/>
对此我能做些什么吗?我在这个问题中看到您可以使用@XmlType(propOrder={"c","b", "a"})
or @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
,但我认为这些不适用于属性。
我的编组代码如下:
public String writeClass(final Foo objectToBeMarshalled) throws JAXBException{
// marshal
final JAXBContext context = JAXBContext.newInstance(Foo.class);
final Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
final StringWriter writer = new StringWriter();
marshaller.marshal(objectToBeMarshalled, writer);
return writer.toString();
}