1

在我的代码中,我有这个 bean:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Data implements Serializable {

@XmlAttribute
private String _name;

@XmlElementWrapper(name = "statuses")
@XmlElement(name="status")
    private List<Boolean> _statuses;

public String getName() {
    return _name;
}

public void setName(String name) {
    _name = name;
}

    public List<Boolean> getStatuses() {
        return _statuses;
    }

    public void setStatuses(List<Boolean> statuses) {
        _statuses = statuses;
    }

 ....

}

这是编组 bean 的代码:

Writer w = null;
    try {
        final JAXBContext context = JAXBContext.newInstance(Data.class);
        final Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        w = new FileWriter("file.xml");
        m.marshal(obj, w);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try { w.close();}
        catch (Exception e) {}
    }

我期望的是:

<data name="data-test">
<statuses>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
</statuses>
...
</data>

但我得到了:

<data name="data-test">
<statuses>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
    <status>true</status>
</statuses>

// strange repetition
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>

// attribute as separated element
<name>data-test</name>
...
</data>

我怎样才能避免那种奇怪的重复呢?

4

2 回答 2

0

我的猜测是访问类型不起作用。你试过了XmlAccessType.FIELD吗?另外,请确保您已经正确编译了您的类并且您的类路径设置正确(也许您有一个旧类从较早的测试中得到?)。

更新:

您可以尝试显式添加@XmlTransient到 getter/setter 吗?

于 2012-04-28T13:35:34.427 回答
0

它看起来像 JDK 1.6.24 的内置 jaxb 中的错误

于 2012-04-28T15:45:57.340 回答