1

In my code I have this 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;
    }

 ....

}

This is the code to marshal the 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) {}
    }

What I expect is:

<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>

but I got:

<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>

How can I avoid that strange kind of repetitions?

0

2 Answers 2

0

my guess is that the access type isn't working. did you try XmlAccessType.FIELD? also, be sure you have correctly compiled your class and that your classpath is setup correctly (maybe you have an old class lying around from an earlier test?).

UPDATE:

can you try explicitly adding @XmlTransient to the getters/setters?

5
  • Yes but I get exactly the same result :(
    – Randomize
    Apr 28, 2012 at 13:37
  • @Randomize - added another suggestion.
    – jtahlborn
    Apr 28, 2012 at 13:42
  • adding transient on get/set I get: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions...
    – Randomize
    Apr 28, 2012 at 13:45
  • @Randomize - what version of java are you using and are you using the default (built in) jaxb implementation?
    – jtahlborn
    Apr 28, 2012 at 15:01
  • Yes built in version in JDK 1.6.24. I just switched to Java 7 and the problem seems gone! BTW java 6 should be got to the version 1.6.31. It looks to me a version problem at this point.
    – Randomize
    Apr 28, 2012 at 15:09
0

it looks like a bug in built in jaxb for JDK 1.6.24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.