2

Using com.sun.xml.bind jaxb-impl 2.2.6.

I've got parent and child object, what I would like to get after marshall is:

<parent/> or <parent></parent>

But what I get is an error like: Caused by:

com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: com.test.data.Child@....

Here is the code:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"content"})
@XmlRootElement(name = "child")
public class Child {
    @XmlValue
    protected String content;
    public String getContent() {
        return content;
    }
}
...

Then

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"child"})
@XmlRootElement(name = "parent")
public class Parent {
    protected Child child;
    public Child getChild() {
        return child;
    }
}

What I do is (that's because I want to be able to "maybe" fill the content of child later):

if(parent.getChild == null)
    parent.setChild(new Child());

Then, when I try to marshall, I get the error seen above.

If I try:

if(parent.getChild == null) {
    Child child = new Child();
    child.setContent("");
    parent.setChild(child);
}

Then I get the expected:

<parent><child></child></parent>

And I can get what I want by never setting the child object in its parent (so no parent.setChild(...)), but as stated above, it would save me time to set it earlier.

Basically I am trying to do exactly the opposite as in the post: JAXB: Empty string does not produce empty element

Is there anything I am doing wrong, or maybe something I did not setup correctly?

Thanks,

Yoann

Edit: Is there any other way than to either:
- Set it back to null later: parent.setChild(null);
- Never set it as an object in the first place

Community
  • 1
  • 1
Yoann
  • 51
  • 4

0 Answers0