[英]Trying to serialize an object compactly using Castor
I'm using Castor to write out a map of user ID's to time intervals. 我正在使用Castor写出用户ID到时间间隔的映射。 I'm using it to save and resume progress in a lengthy task, and I'm trying to make the XML as compact as possible. 我正在使用它来保存和恢复冗长的任务中的进度,并且正在尝试使XML尽可能紧凑。 My map is from string userID's to a class that contains the interval timestamps, along with additional transient data that I don't need to serialize. 我的地图是从字符串userID到包含时间间隔时间戳以及不需要序列化的其他临时数据的类的。
I'm able to use a nested class mapping: 我可以使用嵌套的类映射:
...
<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>
...
And get output that looks like: 并获得如下所示的输出:
<u n="36164639"><value s="1292750896000" e="1292750896000"/></u>
What I'd like is the name, start, and end of the user in a single node like this. 我想要的是这样的单个节点中用户的名称,开始和结束。
<u n="36164639" s="1292750896000" e="1292750896000"/>
But I can't seem to finagle it so the start and end attributes in the "value" go in the same node as the "key". 但是我似乎无法完成它,因此“值”中的开始和结束属性与“键”位于同一节点中。 Any ideas would be greatly appreciated. 任何想法将不胜感激。
Nash, I think to arrange the castor mapping is bit tricky. 纳什,我认为安排脚轮贴图有些棘手。 If you want to have structure like 如果你想拥有这样的结构
<u n="36164639" s="1292750896000" e="1292750896000"/>
Then you need to create a new pojo file where it will be having all the three fields Key,intervalStart,intervalEnd. 然后,您需要创建一个新的pojo文件,它将在其中包含所有三个字段Key,intervalStart,intervalEnd。 And let the File name as KeyTimeInterval And map it like the below. 并将文件名设置为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>
I think you should be able to use location
on s
and e
. 我认为您应该可以在s
和e
上使用location
。 Try this:- 尝试这个:-
...
<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>
Am answering my own question here, since there is a solution that does exactly what I want, and there's actually an error in the explanation at http://www.castor.org/xml-mapping.html#Sample-3:-Using-the-container-attribute - the container attribute is exactly what's needed here. 我在这里回答我自己的问题,因为有一种解决方案可以完全满足我的要求,并且http://www.castor.org/xml-mapping.html#Sample-3:-使用中的说明中实际上存在错误: -the-container-attribute- 容器属性正是这里所需要的。
Changing one line in the mapping: 更改映射中的一行:
<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 . 当然,错误是文档说明您通过将container属性设置为false来执行此操作。 Of course, it should be true . 当然,这应该是真的 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.