org.simpleframework.xml.ElementMap 的正确用法?
我正在尝试使用 org.simpleframework.xml.ElementMap 将以下 XML 映射到我的 Java 类:
<my_map class="java.util.HashMap">
<my_entry id="one" other_attribute="abc">
<my_entry_element>blahblah one</my_entry_element>
</my_entry>
<my_entry id="two" other_attribute="def">
<my_entry_element>blahblah two</my_entry_element>
</my_entry>
</my_map>
但是,我找不到任何解决方案。 我能得到的最接近的是将每个条目包含在冗余的
像这样:
<my_map class="java.util.HashMap">
<entry id="one">
<my_entry id="one" other_attribute="abc">
<my_entry_element>blahblah one</my_entry_element>
</my_entry>
</entry>
<entry id="two">
<my_entry id="two" other_attribute="def">
<my_entry_element>blahblah two</my_entry_element>
</my_entry>
</entry>
</my_map>
上面的 XML 片段与以下 Java 包装器配合得很好:
@Root(name="my_root_class")
public class MyRootClass {
@ElementMap(name="my_map"
,key="id"
,keyType=String.class
,valueType=MyEntry.class
,attribute=true
,inline=false
)
private Map<String, MyEntry> myEntries = new HashMap<String, MyEntry>();
// ... (getters/setters/..)
}
元素映射正确:
MyRootClass [
two: MyEntry [id=two, otherAttribute=def, myEntryElement=blahblah two]
one: MyEntry [id=one, otherAttribute=abc, myEntryElement=blahblah one]
]
然后,我尝试设置“inline=true”并删除多余的
。 如果我设置 inline="true"、entry="my_entry",并使用我在此消息顶部引入的第一个 XML(真正的 XML我希望能够使用),但出现错误:
ExceptionUnable to satisfy @org.simpleframework.xml.ElementMap(keyType=class java.lang.String, inline=true, entry=my_entry, name=my_map, data=false, empty=true, value=, attribute=true, valueType=class com.mycomp.thomas.simpleXml.MyEntry, required=true, key=id) on field 'myEntries' private java.util.Map com.mycomp.thomas.simpleXml.MyRootClass.myEntries for class com.mycomp.thomas.simpleXml.MyRootClass at line 1
我还尝试使用 value="my_entry" 甚至重命名 XML 中的
文件到
(默认),没有任何作用。
有人可以告诉我在 @ElementMap 中使用的权限参数,以使本文顶部介绍的 XML 正常工作吗?
I am trying to use org.simpleframework.xml.ElementMap to map the following XML to my Java classes:
<my_map class="java.util.HashMap">
<my_entry id="one" other_attribute="abc">
<my_entry_element>blahblah one</my_entry_element>
</my_entry>
<my_entry id="two" other_attribute="def">
<my_entry_element>blahblah two</my_entry_element>
</my_entry>
</my_map>
However, I could not find any solution.
The closer I could get is to enclose each entry inside a redundant <entry id="xyz"> ... </entry>
like this:
<my_map class="java.util.HashMap">
<entry id="one">
<my_entry id="one" other_attribute="abc">
<my_entry_element>blahblah one</my_entry_element>
</my_entry>
</entry>
<entry id="two">
<my_entry id="two" other_attribute="def">
<my_entry_element>blahblah two</my_entry_element>
</my_entry>
</entry>
</my_map>
The above piece of XML works well with the following Java wrapper:
@Root(name="my_root_class")
public class MyRootClass {
@ElementMap(name="my_map"
,key="id"
,keyType=String.class
,valueType=MyEntry.class
,attribute=true
,inline=false
)
private Map<String, MyEntry> myEntries = new HashMap<String, MyEntry>();
// ... (getters/setters/..)
}
The elements are mapped correctly:
MyRootClass [
two: MyEntry [id=two, otherAttribute=def, myEntryElement=blahblah two]
one: MyEntry [id=one, otherAttribute=abc, myEntryElement=blahblah one]
]
Then, I try to set "inline=true" and remove the redundant <entry>
.
If I set inline="true", entry="my_entry", and use the first XML that I introduced at the top of this message (the real one, the one I would like to be able to use), I get an error:
ExceptionUnable to satisfy @org.simpleframework.xml.ElementMap(keyType=class java.lang.String, inline=true, entry=my_entry, name=my_map, data=false, empty=true, value=, attribute=true, valueType=class com.mycomp.thomas.simpleXml.MyEntry, required=true, key=id) on field 'myEntries' private java.util.Map com.mycomp.thomas.simpleXml.MyRootClass.myEntries for class com.mycomp.thomas.simpleXml.MyRootClass at line 1
I also tried playing with the value="my_entry" or even renaming <my_entry>
in the XML file to <entry>
(the default one), nothing works.
Can someone tell me the rights parameters to use in the @ElementMap to make the XML introduced at the very top of this post work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到过这个问题。
不幸的是,当前版本的简单框架(2.7)似乎不可能实现所需的紧凑序列化。
请注意,有一个几年前仍未合并的补丁,可以将原始值序列化为条目属性:
https://sourceforge.net/tracker/index.php?func=detail&aid=3032849&group_id=112203&atid=661528
I came across this issue too.
Unfortunately, the desired compact serialization seems to be impossible with current version of Simple Framework (2.7).
Note that there is a several-years-old still-unmerged patch enabling serialization of primitive values as entry attributes:
https://sourceforge.net/tracker/index.php?func=detail&aid=3032849&group_id=112203&atid=661528