Friday, 30 January 2015

Simple XML @ElementMap unable to satisfy key attribute



I'm trying to deserialise with Simple XML 2.6.2 using the @ElementMap annotation to build a map containing an element's attribute as the key and the element itself as the value.


The XML looks like this:



<ProcessConfiguration id="4020">
<EquipmentConfigurations>
<EquipmentConfiguration id="5020">
<address>foo</address>
</EquipmentConfiguration>
</EquipmentConfigurations>
</ProcessConfiguration>


The annotated classes look like this:



@Root
class ProcessConfiguration {

@Attribute
Long id;

@ElementMap(name = "EquipmentConfigurations", key="id", attribute = true)
Map<Long, EquipmentConfiguration> equipmentConfigurations = new HashMap<>();
}


EquipmentConfiguration.java



@Root
class EquipmentConfiguration {

@Attribute
Long id;

@Element
String address;
}


As you can see, the equipmentConfigurations map should contain the EquipmentConfiguration ID as the map key, and the EquipmentConfiguration as the map value.


But when I try to deserialise, the following error is thrown:



Exception in thread "main" org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(required=true, empty=, name=) on field 'id' java.lang.Long EquipmentConfiguration.id for class EquipmentConfiguration at line 1


I've tried various things with the @ElementMap annotation, but without success.


I'm pretty stuck here, as I don't see how Simple isn't finding the id attribute. Are there any Simple wizards out there who can help?


Thanks in advance!


No comments:

Post a Comment