给定以下XML:

<Location xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Exits>
    <string>/Maze/Location/easy/00f744f5-9737-4460-9791-9b44013346b7</string>
  </Exits>
  <LocationId>ebd65e24-ec5a-4105-8738-192da75b46eb</LocationId>
  <LocationType>Start</LocationType>
</Location>


我正在尝试将XML编组为以下pojo:

@XmlRootElement(name = "Location")
public class Location {
    private List<String> exits = new ArrayList<String>();
    private String locationId;

    @XmlElement(name = "Exits")
    public void setExits(List<String> exits) {
        this.exits = exits;
    }

    public List<String> getExits() {
        return exits;
    }
    private String locationType;

    @XmlElement(name = "LocationId")
    public void setLocationId(String locationId) {
        this.locationId = locationId;
    }

    public String getLocationId() {
        return locationId;
    }

    @XmlElement(name = "LocationType")
    public void setLocationType(String locationType) {
        this.locationType = locationType;
    }

    public String getLocationType() {
        return locationType;
    }
}


将设置locationId和locationType的值,但不会将“退出”列表解析为对象中相应的List。当前,“退出”列表包含一个带有“ \ n”的条目,而不是“ / Maze / Location / easy / 00f744f5-9737-4460-9791-9b44013346b7”

我如何配置pojo才能正确解析它。

我正在使用Spring和JaxB。

最佳答案

@XmlElementWrapper巧妙地将列表的所有元素包装到另一个元素中。

@XmlElementWrapper(name="Exits")
@XmlElement(name = "string")
public void setExits(List<String> exits) {
    this.exits = exits;
}

09-27 00:28
查看更多