如何告诉castor将空字段编组到空标签?
我正在编组一个可以将某些字段设置为空的对象。我使用带有 xml 映射文件的 Castor 进行配置。我正在编组的类是这样的:
class Entity {
private int id;
private String name;
private String description; // THIS CAN BE NULL
/* ... getters and setters follow ... */
}
...以及这样的映射文件:
<mapping>
<class name="Entity">
<field name="id" type="integer"/>
<field name="name" type="string"/>
<field name="description" type="string"/>
</class>
</mapping>
如果字段为空,我现在会得到什么(简化的示例):
<entity>
<id>123</id>
<name>Some Name</name>
</entity>
而我想在生成的 XML 中有一个空标签,即使描述字段为空。
<entity>
<id>123</id>
<name>Some Name</name>
<description /> <!-- open/close tags would be ok -->
</entity>
I'm marshalling an object that can have some field set to null. I'm using castor with a xml-mapping file for the configuration. The class I'm marshalling is like this:
class Entity {
private int id;
private String name;
private String description; // THIS CAN BE NULL
/* ... getters and setters follow ... */
}
...and a mapping file like this:
<mapping>
<class name="Entity">
<field name="id" type="integer"/>
<field name="name" type="string"/>
<field name="description" type="string"/>
</class>
</mapping>
What I'm getting at the moment if the field is null (simplified example):
<entity>
<id>123</id>
<name>Some Name</name>
</entity>
while I want to have an empty tag in the resulting XML, even if the description field is null.
<entity>
<id>123</id>
<name>Some Name</name>
<description /> <!-- open/close tags would be ok -->
</entity>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现此目的的一种方法是使用 GeneralizedFieldHandler。这有点麻烦,但它适用于其他字符串字段。
例子:
One way to do this is with a GeneralizedFieldHandler. It's a bit of a hack but it will work for other fields that are Strings.
Example: