4

I made some classes using xjc.

    public class MyType {

    @XmlElementRefs({
        @XmlElementRef(name = "MyInnerType", type = JAXBElement.class, required = false),

    })
    @XmlMixed
    protected List<Serializable> content;

    public List<Serializable> getContent() {
        if (content == null) {
            content = new ArrayList<Serializable>();
        }
        return this.content;
    }
}

But i cant add inner elements using

getContent().add(newItem);

because MyInnerType is not Serializable. Why its not a List of Objects? How do i add inner elements?

bunnyjesse112
  • 747
  • 6
  • 27
  • 44

3 Answers3

5

Please take a look here and here (one should for sure address your scenario).

From 2nd link:

<!-- schema fragment having  mixed content -->
<xs:complexType name="letterBody" mixed="true">
<xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="quantity" type="xs:positiveInteger"/>
    <xs:element name="productName" type="xs:string"/>
    <!-- etc. -->
</xs:sequence>
</xs:complexType>
<xs:element name="letterBody" type="letterBody"/>


// Schema-derived Java code: 
// (Only annotations relevant to mixed content are shown below, 
//  others are ommitted.)
import java.math.BigInteger;
public class ObjectFactory {
    // element instance factories
    JAXBElement<LetterBody> createLetterBody(LetterBody value);
    JAXBElement<String>     createLetterBodyName(String value);
    JAXBElement<BigInteger> createLetterBodyQuantity(BigInteger value);
    JAXBElement<String>     createLetterBodyProductName(String value);
  // type instance factory
    LetterBody> createLetterBody();
}

public class LetterBody {
    // Mixed content can contain instances of Element classes
    // Name, Quantity and ProductName. Text data is represented as
    // java.util.String for text.
    @XmlMixed 
    @XmlElementRefs({
            @XmlElementRef(name="productName", type=JAXBElement.class),
            @XmlElementRef(name="quantity", type=JAXBElement.class),
            @XmlElementRef(name="name", type=JAXBElement.class)})
    List getContent(){...}
}
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
1

What do you think you should be adding in there? I have used similar generation and had fields like this and the expectation was that it would be String content.

It'd probably help to show the xsd this was generated from.

AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
  • Thanks for answer. MyInnerType is not a String. Why it generates List and not List? Do i need to wrap MyInnerType in JAXBElement or something? – bunnyjesse112 Jan 31 '12 at 15:13
  • 1
    It sounds like that would be a good idea since your object isn't serializable. But why not make your object serializable? It's just a matter of implementing the interface. – AHungerArtist Jan 31 '12 at 16:09
0

May be helpful for smbd now. You have to use:

getContent().add(new JAXBElement<>(new QName("MyInnerType"), MyInnerType.class, myInnerTypeInstance);
Yuri Smirnoff
  • 469
  • 1
  • 5
  • 10