Proper use of org.simpleframework.xml.ElementMap?

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 a solution. The closer I could conclude, is to enclose each entry inside the redundant <entry id="xyz"> ... </entry> as follows:

 <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 XML fragment works well with the following Java shell:

 @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/..) } 

Elements are displayed 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 entered at the beginning of this post (real, that I would like to be able to use), I get an error message:

 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> (default), nothing works.

Can someone tell me the permissions for use in @ElementMap to make the XML presented in the very top corner of this post work?

+4
source share
1 answer

I also ran into this problem.

Unfortunately, the desired compact serialization is apparently not possible in the current version of Simple Framework (2.7).

Please note that there is a multi-year fixed patch that allows you to serialize primitive values ​​as record attributes: https://sourceforge.net/tracker/index.php?func=detail&aid=3032849&group_id=112203&atid=661528

+3
source

Source: https://habr.com/ru/post/1393672/


All Articles