Reputation: 605
I have the XML:
<?xml version="1.0" encoding="UTF-8"?>
<addresses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='schema_task1_1.xsd'>
<address>
<name>Bill</name>
<city>NewYork</city>
<street>First</street>
<phone type='mobile'>+19139130000</phone>
<documents>
<document type='driving_permit'>9045798749</document>
<document type='passport'>3451231231</document>
</documents>
</address>
<address>
...
</address>
</addresses>
And the Java classes:
@Root(name = "addresses")
public class Addresses {
@ElementList(inline = true, entry = "address", required = false, type = Address.class)
private List<Address> addresses = new ArrayList<Address>();
public class Address {
@Element(required = false)
private String name;
@Element(required = false)
private String city;
@Element(required = false)
private String street;
@ElementMap(entry = "phone", key = "type", attribute = true, inline = true)
private Map<String, String> phones;
@ElementMap(entry = "documents", key = "type", attribute = true, inline = true)
private Map<String, String> documents = new HashMap<String, String>();
All fields deserialize successful, except documents
. It is always null
when try to deserialize. I thin I have error in @ElementMap
annotation, but I don't know what exactly. What am I doing wrong?
PS. I can't change object structure because it is used by another parsers.
Upvotes: 2
Views: 793
Reputation: 605
I resolved my problem, but not too nice. If you know better solution, please, let me know.
I just fixed annotation for document
Map
and added uselessVariable
with annotation. It works and doesn't break old code.
UPD. I found better solution. Added @Path
annotation.
@Root(name = "addresses")
public class Addresses {
@ElementList(inline = true, entry = "address", required = false, type = Address.class)
private List<Address> addresses = new ArrayList<Address>();
public class Address {
@Element(required = false)
private String name;
@Element(required = false)
private String city;
@Element(required = false)
private String street;
@ElementMap(entry = "phone", key = "type", attribute = true, inline = true)
private Map<String, String> phones;
@Path("./documents")
@ElementMap(entry = "document", key = "type", attribute = true, inline = true) //changed this line
private Map<String, String> documents = new HashMap<String, String>();
Upvotes: 0
Reputation: 581
You must create an intermediate class to handle "documents". This class will contain you map of "document":
class Address {
...
@Element(required = false)
private Documents documents;
}
class Documents {
@ElementMap(entry = "document", key = "type", attribute = true, inline = true)
private Map<String, String> documents = new HashMap<String, String>();
}
Upvotes: 1