如何收集列表中的所有xml元素?

时间:2017-06-15 13:19:49

标签: java spring spring-boot jackson-dataformat-xml

我正在使用jackson-dataformat-xml

我有以下课程:

public class CTHotel {
    @JacksonXmlProperty(localName = "basic-info")
    private HotelBaseInfo hotelBaseInfo;

    //other properties and getters and setters
}

public class HotelBaseInfo {
    @JacksonXmlProperty(localName = "hotel-name")
    private String hotelName;

    @JacksonXmlElementWrapper(localName = "hotel-amenities")
    private List<HotelAmenity> hotelAmenities;

    //other properties and getters/setters
}

public class HotelAmenity {
    private String category;
    private String description;

    @JacksonXmlElementWrapper(localName = "amenities")
    private List<String> amenities;

    //other properties and getters/setters
}

我的XML是这样的:

<hotels>
    <hotel>
        <basic-info>
            <hotel-name>Hotel XYZ</hotel-name>
            <hotel-amenities>
                <hotel-amenity>
                    <category>F&B</category>
                    <description>Random Text</description>
                    <amenities>
                        <amenity>Cafe</amenity>
                        <amenity>Bar</amenity>
                        <amenity>Rastaurant</amenity>
                    </amenities>
                </hotel-amenity>
                <hotel-amenity>
                ...
                </hotel-amenity>   
            </hotel-amenities>
        </basic-info>
    </hotel>
    <hotel>
    ...
    </hotel>
</hotels>

我的问题是,如何将amenities映射为我HotelAmenity类中的字符串列表,如上所述?我应该在amenities字段上使用什么注释?

@JacksonXmlElementWrapper类的hotelAmenities字段上的

Hotel注释工作正常。

映射时出现以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: java.io.StringReader@507bcc81; line: 3, column: 1039] (through reference chain: com.example.response.HotelSearchResponse["hotels"]->java.util.ArrayList[2]->com.example.response.CTHotel["basic-info"]->com.example.response.HotelBaseInfo["hotel-amenities"]->java.util.ArrayList[1]->com.example.response.HotelAmenity["amenities"]->java.util.ArrayList[9])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) ~[jackson-databind-2.6.5.jar:2.6.5]
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857) ~[jackson-databind-2.6.5.jar:2.6.5]
...

2 个答案:

答案 0 :(得分:0)

这对我有用:

public class JacksonXmlParsing {

@JacksonXmlRootElement(localName = "hotels")
static class HotelSearchResponse {

    @JacksonXmlElementWrapper(localName = "hotel")
    private List<CTHotel> hotels;

    //other properties and getters and setters
}

static class CTHotel {

    @JacksonXmlProperty(localName = "hotel-name")
    private String hotelName;

    @JacksonXmlElementWrapper(localName = "hotel-amenities")
    private List<HotelAmenity> hotelAmenities;

    //other properties and getters and setters
}

static class HotelAmenity {
    private String category;
    private String description;

    @JacksonXmlElementWrapper
    private List<String> amenities;

    //other properties and getters/setters
}

    public static void main(String[] args) throws IOException {
        XmlMapper xmlMapper = new XmlMapper();

        File file = new File("./src/main/resources/file.xml");
        HotelSearchResponse response = xmlMapper.readValue(file, HotelSearchResponse.class);
        System.out.println(response);
    }

}

输出: HotelSearchResponse(hotels=[CTHotel(hotelName=Hotel XYZ, hotelAmenities=[HotelAmenity(category=F&B, description=Random Text, amenities=[Cafe, Bar, Rastaurant])])])

但错过basic-info标记,我可以找出原因。

答案 1 :(得分:0)

这是代码,我希望能回答您的问题:

/**Class Hotels*/
@JacksonXmlRootElement(localName = "hotels")
public class Hotels {

    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Hotel> hotel;

    //Other getters and setters
}

/**Class Hotel*/
@JacksonXmlRootElement(localName = "hotel")
public class Hotel {
    @JacksonXmlProperty(localName = "basic-info")
    private HotelBaseInfo hotelBaseInfo;

    //Other getters and setters
}

/**Class HotelBaseInfo*/
public class HotelBaseInfo {
    @JacksonXmlProperty(localName = "hotel-name")
    private String hotelName;

    @JacksonXmlElementWrapper(localName = "hotel-amenities")
    private List<HotelAmenity> hotelAmenities;

    //Other getters and setters

}

/**Class HotelAmenity*/
public class HotelAmenity {
    private String category;
    private String description;

    @JacksonXmlElementWrapper(localName = "amenities")
    private List<Amenities> amenity;

    static class Amenities {
        @JacksonXmlText
        private String value;
    }

    //Other getters and setters
}