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

Java 如何收集列表中的所有xml元素?,java,spring,spring-boot,jackson-dataformat-xml,Java,Spring,Spring Boot,Jackson Dataformat Xml,我使用的是jackson数据格式xml 我有以下课程: public class CTHotel { @JacksonXmlProperty(localName = "basic-info") private HotelBaseInfo hotelBaseInfo; //other properties and getters and setters } public class HotelBaseInfo { @JacksonXmlProperty(loca

我使用的是
jackson数据格式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
}

以下是对我有效的方法:

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);
    }

}
公共类JacksonXmlParsing{
@JacksonXmlRootElement(localName=“hotels”)
静态类HotelSearchResponse{
@JacksonXmlElementWrapper(localName=“酒店”)
私人列表酒店;
//其他属性以及getter和setter
}
静态级酒店{
@JacksonXmlProperty(localName=“酒店名称”)
私人字符串酒店名称;
@JacksonXmlElementWrapper(localName=“酒店设施”)
私人列表酒店;
//其他属性以及getter和setter
}
静态类酒店{
私有字符串类别;
私有字符串描述;
@JacksonXmlElementWrapper
私人设施清单;
//其他属性和getter/setter
}
公共静态void main(字符串[]args)引发IOException{
XmlMapper XmlMapper=新的XmlMapper();
File File=新文件(“./src/main/resources/File.xml”);
HotelSearchResponse响应=xmlMapper.readValue(文件HotelSearchResponse.class);
System.out.println(响应);
}
}
输出:
HotelSearchResponse(hotels=[CTHotel(hotelName=Hotel XYZ,hotelamanities=[hotelamanity(category=F&B,description=Random Text,commercies=[Cafe,Bar,Rastaurant])))


但是
基本信息
标签丢失了,我可以找出原因。

以下是对我有效的方法:

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);
    }

}
公共类JacksonXmlParsing{
@JacksonXmlRootElement(localName=“hotels”)
静态类HotelSearchResponse{
@JacksonXmlElementWrapper(localName=“酒店”)
私人列表酒店;
//其他属性以及getter和setter
}
静态级酒店{
@JacksonXmlProperty(localName=“酒店名称”)
私人字符串酒店名称;
@JacksonXmlElementWrapper(localName=“酒店设施”)
私人列表酒店;
//其他属性以及getter和setter
}
静态类酒店{
私有字符串类别;
私有字符串描述;
@JacksonXmlElementWrapper
私人设施清单;
//其他属性和getter/setter
}
公共静态void main(字符串[]args)引发IOException{
XmlMapper XmlMapper=新的XmlMapper();
File File=新文件(“./src/main/resources/File.xml”);
HotelSearchResponse响应=xmlMapper.readValue(文件HotelSearchResponse.class);
System.out.println(响应);
}
}
输出:
HotelSearchResponse(hotels=[CTHotel(hotelName=Hotel XYZ,hotelamanities=[hotelamanity(category=F&B,description=Random Text,commercies=[Cafe,Bar,Rastaurant])))


但是
基本信息
标签丢失了,我可以找出原因。

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

/**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
}
/**级酒店*/
@JacksonXmlRootElement(localName=“hotels”)
公共级酒店{
@JacksonXmlElementWrapper(useWrapping=false)
私人列表酒店;
//其他的接球手和二传手
}
/**一流酒店*/
@JacksonXmlRootElement(localName=“酒店”)
公务舱酒店{
@JacksonXmlProperty(localName=“基本信息”)
私人HotelBaseInfo HotelBaseInfo;
//其他的接球手和二传手
}
/**Class HotelBaseInfo酒店*/
公共类HotelBaseInfo酒店{
@JacksonXmlProperty(localName=“酒店名称”)
私人字符串酒店名称;
@JacksonXmlElementWrapper(localName=“酒店设施”)
私人列表酒店;
//其他的接球手和二传手
}
/**等级酒店*/
公营酒店{
私有字符串类别;
私有字符串描述;
@JacksonXmlElementWrapper(localName=“便利设施”)
私人列表设施;
静态级便利设施{
@JacksonXmlText
私有字符串值;
}
//其他的接球手和二传手
}

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

/**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
}
/**级酒店*/
@JacksonXmlRootElement(localName=“hotels”)
公共级酒店{
@JacksonXmlElementWrapper(useWrapping=false)
私人列表酒店;
//其他的接球手和二传手
}
/**一流酒店*/
@JacksonXmlRootElement(localName=“酒店”)
公务舱酒店{
@JacksonXmlProperty(localName=“基本信息”)
私人HotelBaseInfo HotelBaseInfo;
//其他的接球手和二传手
}
/**Class HotelBaseInfo酒店*/
公共类HotelBaseInfo酒店{
@JacksonXmlProperty(localName=“酒店名称”)
私人字符串酒店名称;
@JacksonXmlElementWrapper(localName=“酒店设施”)
私人列表酒店;
//其他的接球手和二传手
}
/**等级酒店*/
公营酒店{
私有字符串类别;
私有字符串描述;
@JacksonXmlElementWrapper(localName=“便利设施”)
私人列表设施;
静态级便利设施{
@JacksonXmlText
私有字符串值;
}
//其他的接球手和二传手
}

我已编辑了我的问题。原来我的问题并没有得到我想要的答案。现在请看一看。我还提到了我得到的错误。@mantri你只有这三门课吗?您能提供用于解析给定xml的代码吗?酒店列表再次包装在
HotelSearchResponse
类中。这就是我映射xml的方式:
XmlMapper-mapper=newxmlmapper()
HotelSearchResponse HotelSearchResponse=mapper.readValue(xmlStr,HotelSearchResponse.class)
对于
@jacksonxmlementwrapper(localName=“hotel”)
您使用了子标签的名称作为
localName
,但是对于
@jacksonxmlementwrapper(localName=“hotel professionals”)
您使用了包装标签的名称作为
localName
。为什么?我已经编辑了我的问题。原来我的问题并没有得到我想要的答案。现在请看一看。我还提到了我得到的错误。@mantri你只有这三门课吗?您能提供用于解析给定xml的代码吗?酒店列表是aga