本文介绍了marshall with xjc创建了嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我使用xml-> xsd和xjc从中创建类。

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

现在我想创建我的ProductInformation对象,然后对其进行编组。

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

我的问题是xjc创建3个类和一个objectfactory,还有一些ProductInformation中的嵌套类。当我看到可用的方法时,我主要看到getter而不是setter。

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();

Jaxb这是正常的吗?

Is this normal with Jaxb?

推荐答案

JAXB通常处理多值属性的方式是为 List< Whatever> 提供一个getter而不是setter,它返回一个可变列表 - 您应该调用getter来检索最初为空的列表,然后以正常方式使用 new 创建此列表的成员对象,并且直接添加到列表中。您可以 new 一个静态嵌套类,其方式与顶级类完全相同。

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.

单值应该使用getter和setter生成属性(非列表)。

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

这篇关于marshall with xjc创建了嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:15