简体   繁体   English

正确使用org.simpleframework.xml.ElementMap?

[英]Right usage of org.simpleframework.xml.ElementMap?

I am trying to use org.simpleframework.xml.ElementMap to map the following XML to my Java classes: 我正在尝试使用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>

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: 我越接近将每个条目包含在冗余的<entry id="xyz"> ... </entry>如下所示:

<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: 上面的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/..)

}

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> . 然后,我尝试设置“inline = true”并删除冗余的<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: 如果我设置inline =“true”entry =“my_entry” ,并使用我在此消息顶部引入的第一个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

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. 我也尝试使用value =“my_entry”或者甚至将XML文件中的<my_entry>重命名为<entry> (默认值),没有任何效果。

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? 有人能告诉我在@ElementMap中使用的权限参数,以便在本文后面的顶部引入XML吗?

I came across this issue too. 我也遇到过这个问题。

Unfortunately, the desired compact serialization seems to be impossible with current version of Simple Framework (2.7). 不幸的是,当前版本的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 请注意,有一个几年前仍然未合并的补丁,可以将原始值序列化为条目属性: https ://sourceforge.net/tracker/index.php?func = enter&aid = 3032849&group_id = 112203 &atid = 661528

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM