Castor XML 映射和 java.util.Map
在过去的几天里,我一直在使用 Castor 来尝试在我的 Java 程序之间进行一些序列化和 XML 以可读的方式。虽然有一些缺陷,但 Castor 通过反射自动生成 xml 实际上非常实用。不幸的是,示例中似乎相当遗漏的一件事是处理泛型。看起来反射 API 确实做得很好,但由于方法以 get___()
开头,它无意中抓取了很多冗余数据,我想编写自己的映射文件来保存这关。
首先,在“field
”元素的属性中,应该定义“type
”,这似乎是完全公平的。但是,它没有指定如果此类型是抽象类型或只是一个接口应该做什么。那我应该把什么作为类型呢?
其次,Castor 中指定的大多数“集合”类型对象(List
、Vector
、Collection
、Set
等)仅需要 1 个泛型类型,因此指定“type
”作为内部内容和“collection="true"
”就足够了。但是,它没有指定在像 Map
这样的集合(其中需要 2 种类型)的情况下我应该做什么。如何同时指定键类型和值类型?
任何帮助将不胜感激!
I've been using Castor these past couple of days to try to get a little serialization going between my Java program and XML in a readable way. Though it has a few faults, Castor's automatic xml generation via reflection is actually very functional. Unfortunately, one thing that seems to be fairly well left out of the examples is dealing with generics. It seems the reflection API does a wonderful job as it is, but as it is inadvertently grabbing a lot of redundant data just because methods start with get___()
, I wanted to write my own mapping file to stave this off.
Firstly, it seems altogether fair that in the attributes to a "field
" element, one should define "type
". However, it does not specify what should be done if this type is abstract or simply an interface. What should I put as the type then?
Secondly, most "collection" type objects specified in Castor (List
, Vector
, Collection
, Set
, etc) only require 1 generic type, so specifying "type
" as what's inside and "collection="true"
" are enough. However, it does not specify what I should do in the case of a collection like a Map
, where 2 types are necessary. How can I specify both the key type and value type?
Any help at all would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于我的第二个问题:
当使用地图或表格指定某些内容时,您需要在
bind-xml
元素内重新定义org.exolab.castor.mapping.MapItem
在您的field
元素中。示例取自此处此外,省略
type
属性父field
元素。For the second of my questions:
When specifying something with a Map or a Table, you need to redefine
org.exolab.castor.mapping.MapItem
within thebind-xml
element within yourfield
element. Example taken from hereAlso, omit the
type
attribute from the parentfield
element.对于我的第一个问题,技巧是不要指定字段元素中的类型并允许 Castor 自行推断它。如果您有可能出现在那里的类的定义,那么它将自动使用这些定义。例如:
Castor 的 into XML 输出将使用条件 1 和条件 2 样式 XML 进入 Clazz 的“条件”字段,同时仍然引用其正确的实例化类型。
For my first question, the trick is to NOT specify the type in the field element and allow Castor to infer it by itself. If you have definitions for the classes that could appear there, then it will automatically use those. For example:
The output of into XML by Castor would use condition1 and condition2 style XML into the "condition" field of Clazz while still referring to its proper instantiation type.