我正在使用 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"/>
但我似乎无法解决它,因此“值”中的开始和结束属性与“键”位于同一节点中。任何想法将不胜感激。