Search code examples
javajaxbxjc

marshall with xjc created nested classes


<ProductInformation Context="GL">
 <Assets>
  <Asset ID="assetID" UserTypeID="ID">
    <Name>name</Name>
    <Reference ClassificationID="id"/>
      <Values>
        <Value AttributeID="ID">Value1</Value>
        <Value AttributeID="ID">Value2</Value>
          <MultiValue AttributeID="attributeID">
             <Value>value3a</Value>
             <Value>value3b</Value>
          </MultiValue>
     </Values>
   </Asset>
 </Assets>

 <Products>....</Products>

</ProductInformation>

I used this xml->xsd and xjc to create classes from it.

Now I want to create my ProductInformation object,and marshall it.

My problem is xjc create 3 classes and a objectfactory, and some nested classes inside ProductInformation. When I look at the avaliable methods I mostly see getters instead of setters.

"Asset" class has no such methods like;

asset.setValues(List<Value> values)

Also I ended up writing funny code like this;

ProductInformation.Assets.Asset.Values.MultiValue multivalue=new ProductInformation.Assets.Asset.Values.MultiValue();

Is this normal with Jaxb?


Solution

  • The way JAXB normally handles multi valued properties is to provide just a getter and no setter for the List<Whatever>, which returns a mutable list - you're supposed to call the getter to retrieve an initially-empty list and then create the member objects for this list using new in the normal way and add them directly to the list. You can new a static nested class in exactly the same way as a top-level one.

    Single-valued properties (not lists) should have been generated with both getter and setter.