如何编组字符串列表

时间:2014-03-15 20:21:20

标签: java xml spring jaxb

给出以下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的值,但它没有解析&#34;退出&#34;的列表。进入对象中的相应List。目前,退出列表包含一个带有&#34; \ n&#34;的条目。而不是&#34; /迷宫/位置/ easy / 00f744f5-9737-4460-9791-9b44013346b7&#34;

如何配置pojo以正确解析它。

我使用的是Spring和JaxB。

1 个答案:

答案 0 :(得分:2)

@XmlElementWrapper可以将列表的所有元素包装到另一个元素中。

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