Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB does not call Setter method

Tags:

java

xml

jaxb

I cannot understand what am I doing wrong. I want to unmarshall an xml using JAXB, but i noticed that setter method wasn't called. I am using Java 1.5. Getters and Setters in Attribute.java class - work correctly, but in Configuration.java class - Setter method doesn't call. Can you please show me where am I wrong?

@XmlRootElement(name="configuration")
@XmlAccessorType(XmlAccessType.NONE)
public class Configuration {    
    public List< Configuration> getItems() {
        return new ArrayList<Attribute>(getMap().values());
    }

    @XmlElement(name="attributes")
    public void setItems(List<Attribute> attributes) {
        getMap().clear();
        for (Attribute attribute : attributes) {
            getMap().put(attribute.getName(), attribute);
        }
    }

    private Map<String, Attribute> map;

    public Map<String, Attribute> getMap() {

        if (map == null) {
            map = new HashMap<String, Attribute>();
        }
        return map;
    }
}

My XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <attributes name="some_name" type="calculation" value="select ? from dual" priority="0"/>
</configuration>
like image 583
edward_wong Avatar asked Mar 18 '23 01:03

edward_wong


1 Answers

If a List is returned from the getter a JAXB impl will use that to add the collection items to instead of creating a new one and setting it via the setter.

The purpose of this is to give you the chance to initialize the implementation of List that best fits your domain model.

like image 109
bdoughan Avatar answered Mar 28 '23 18:03

bdoughan