2

我正在使用 Castor 写出用户 ID 到时间间隔的映射。我正在使用它来保存和恢复一项冗长任务的进度,并且我正在尝试使 XML 尽可能紧凑。我的地图是从字符串用户 ID 到包含间隔时间戳的类,以及我不需要序列化的其他瞬态数据。

我可以使用嵌套类映射:

...
<field name="userIntervals" collection="map">
 <bind-xml name="u">
  <class name="org.exolab.castor.mapping.MapItem">
   <field name="key" type="string"><bind-xml name="n" node="attribute"/></field>
   <field name="value" type="my.package.TimeInterval"/>
  </class>
 </bind-xml>
</field>
...
<class name="my.package.TimeInterval">
 <map-to xml="ti"/>
 <field name="intervalStart" type="long"><bind-xml name="s" node="attribute"/></field>
 <field name="intervalEnd" type="long"><bind-xml name="e" node="attribute"/></field>
</class>
...

并获得如下所示的输出:

<u n="36164639"><value s="1292750896000" e="1292750896000"/></u>

我想要的是像这样的单个节点中用户的名称、开始和结束。

<u n="36164639" s="1292750896000" e="1292750896000"/>

但我似乎无法解决它,因此“值”中的开始和结束属性与“键”位于同一节点中。任何想法将不胜感激。

4

3 回答 3

1

Nash,我认为安排脚轮映射有点棘手。如果你想有这样的结构

<u n="36164639" s="1292750896000" e="1292750896000"/> 

然后,您需要创建一个新的 pojo 文件,其中包含所有三个字段 Key、intervalStart、intervalEnd。并将文件名设为 KeyTimeInterval 并将其映射如下。

 <field name="userIntervals" collection="map">    
  <class name="org.exolab.castor.mapping.MapItem">   
    <field name="u" type="my.package.KeyTimeInterval">
      <bind-xml name="u" node="element"/>
    </field>             
   </class>        
 </field>



<class name="my.package.KeyTimeInterval">  
  <field name="key" type="String">
        <bind-xml name="n" node="attribute"/></field> 
    <field name="intervalStart" type="long">
        <bind-xml name="s" node="attribute"/></field>   
     <field name="intervalEnd" type="long">
      <bind-xml name="e" node="attribute"/></field>   
 </class> 
于 2011-02-28T07:44:32.870 回答
0

我认为您应该能够使用locationonse. 试试这个:-

...

<class name="my.package.TimeInterval">
   <map-to xml="ti"/>
   <field name="intervalStart" type="long">
      <bind-xml name="s" location="u" node="attribute"/>
   </field>
   <field name="intervalEnd" type="long">
      <bind-xml name="e" location="u" node="attribute"/>
   </field>
</class>
于 2011-02-26T00:10:51.327 回答
0

我在这里回答我自己的问题,因为有一个解决方案可以完全满足我的要求,并且在http://www.castor.org/xml-mapping.html#Sample-3:-Using的解释中实际上存在错误-the-container-attribute -容器属性正是这里需要的。

更改映射中的一行:

<field name="value" type="my.package.TimeInterval" container="true"/>

did exactly what I wanted, it didn't create a subelement for the value, just mapped the fields into the existing parent element. Since then, I've used this quite a few times to map multiple-value classes into their parent.

The error of course is the documentation states you do this by setting the container attribute to false. Of course, it should be true.

于 2011-03-21T18:57:20.980 回答