【问题标题】:Jackson deserialize xml fields with the same nameJackson 反序列化同名的 xml 字段
【发布时间】:2019-10-21 08:22:59
【问题描述】:

我想将来自HTTP 请求的XML 响应反序列化为POJOs 列表。我遇到的问题是XML 对包含不同值的元素使用相同的名称“属性”。

<nowplaying-info-list>
    <nowplaying-info mountName="FGDGFD" timestamp="1559761606" type="track">
        <property name="cue_time_duration">
            <![CDATA[262000]]>
        </property>
        <property name="cue_time_start">
            <![CDATA[1559761571830]]>
        </property>
        <property name="cue_title">
            <![CDATA[Marine marchande]]>
        </property>
        <property name="track_album_name">
            <![CDATA[Octobre]]>
        </property>
        <property name="track_artist_name">
            <![CDATA[Les Cowboys Fringants]]>
        </property>
        <property name="track_id">
            <![CDATA[8133]]>
        </property>
        </property>
    </nowplaying-info>
...
@JacksonXmlRootElement(localName = "nowplaying-info")
public class ScheduleItem implements Serializable {
    @JacksonXmlProperty(localName = "property")
    private String song = null;
    private String artist = null;
    private String cover = null;
    private Long datetime = null;

我想将名称为cue_title 的属性序列化为歌曲变量,并将cue_time_start 的属性序列化为日期时间。

【问题讨论】:

    标签: java xml spring jackson xml-deserialization


    【解决方案1】:

    字段和列表之间没有简单的映射。我建议创建单独的模型,将 XML 有效负载反序列化,然后转换为所需的 POJO。下面的例子展示了这个想法:

    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.dataformat.xml.XmlMapper;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
    import java.io.File;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    import org.apache.commons.lang3.StringUtils;
    
    public class XmlApp {
    
        public static void main(String[] args) throws Exception {
            System.out.println(new File(".").getAbsolutePath());
            File xml = new File("./src/main/resources/test.xml");
            XmlMapper xmlMapper = new XmlMapper();
            xmlMapper.setDefaultUseWrapper(false);
            xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    
            TypeReference<List<NowPlayingInfo>> type = new TypeReference<List<NowPlayingInfo>>() {
            };
            List<NowPlayingInfo> infos = xmlMapper.readValue(xml, type);
            List<ScheduleItem> items = infos.stream()
                .map(NowPlayingInfo::getPropertiesAsMap)
                .map(m -> {
                    ScheduleItem item = new ScheduleItem();
                    item.setSong(m.get("cue_title"));
                    item.setDatetime(Long.parseLong(m.get("cue_time_start")));
                    return item;
                }).collect(Collectors.toList());
            items.forEach(System.out::println);
        }
    }
    
    class ScheduleItem {
    
        private String song;
        private String artist;
        private String cover;
        private Long datetime;
    
        //getters, setters, toString
    }
    
    class NowPlayingInfo {
    
        private String mountName;
        private long timestamp;
        private String type;
    
        @JsonProperty("property")
        private List<Property> properties;
    
        public Map<String, String> getPropertiesAsMap() {
            Map<String, String> map = new LinkedHashMap<>();
            properties.forEach(p -> map.put(p.getName(), StringUtils.strip(p.getValue())));
            return map;
        }
    
        //getters, setters, toString
    }
    
    class Property {
    
        @JacksonXmlText
        private String value;
    
        @JacksonXmlProperty(isAttribute = true)
        private String name;
    
        //getters, setters, toString
    }
    

    上面的应用程序为您的XML 打印:

    ScheduleItem{song='Marine marchande', artist='null', cover='null', datetime=1559761571830}
    

    【讨论】:

    • 这绝对解决了我的问题。我想避免创建额外的模型类,但确实没有更好的方法。
    • @user3221287,开销不大,但这种方法的灵活性很大。
    猜你喜欢
    • 2016-02-13
    • 2016-02-12
    • 2020-01-14
    • 2017-11-29
    • 2015-04-24
    • 2018-10-26
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    相关资源
    最近更新 更多