我正在使用 FasterXML 序列化 POJO。我想序列化我的 POJO 列表。当序列化一个signle POJO时,我得到了预期的xml(有一个问题->问题2)这是我的代码:
List<Movie> movies = new ArrayList<>();
// add movies
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
xmlMapper = new XmlMapper(module);
xmlMapper.disable(MapperFeature.AUTO_DETECT_CREATORS,
MapperFeature.AUTO_DETECT_FIELDS,
MapperFeature.AUTO_DETECT_GETTERS,
MapperFeature.AUTO_DETECT_IS_GETTERS,
MapperFeature.AUTO_DETECT_SETTERS,
MapperFeature.USE_GETTERS_AS_SETTERS);
String xml = xmlMapper.writeValueAsString(movies);
我明白了:
<ArrayList>
<item imdbID="tt0077687" title="The Hobbit" year="1977"/>
</ArrayList>
这就是我想要的:
<movies>
<movie imdbID="tt0077687" title="The Hobbit" year="1977"/>
<movie imdbID="tt0077687" title="title2" year="1977"/>
</movies>
或者
<movie imdbID="tt0077687" title="The Hobbit" year="1977"/>
<movie imdbID="tt0077687" title="title2" year="1977"/>
当我连载一部电影时,我得到了这个:
是否有可能得到这个:
<movie imdbID="tt0077687" title="The Hobbit" year="1977"><movie>