有没有办法在使用JAXB进行编组时控制属性的顺序?

时间:2013-01-21 20:18:14

标签: java jaxb

  

可能重复:
  How can I make @XmlAttribute in a special order by using JAXB?

为了便于阅读,我需要我的编组xml看起来像这样:

<tag-name name="name" attr1="value1" attr2="value2"/>

现在,当我编辑它时,它就像这样:

<tag-name attr2="value2" attr1="value1" name="name"/>

我能做些什么吗?我在this question中看到您可以使用@XmlType(propOrder={"c","b", "a"})@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();
    } 

0 个答案:

没有答案