简体   繁体   English

从不同的XML数据集中以相同名称反序列化XML @Element和@ElementList

[英]Deserializing XML @Element and @ElementList with same name from different XML datasets

I want to deserialize different sets of XML serialized data into one class (not at the same time) using SimpleXML . 我想使用SimpleXML将不同系列的XML序列化数据反序列化为一个类(而不是同时)。

All of the datasets have a root class of <body/> , but different subclasses. 所有数据集的根类均为<body/> ,但子类不同。 Some have one element of <prediction> , other multiple elements of <prediction> . 一些具有一个元素<prediction> ,的其他多个元件<prediction> Same for <route> . <route>相同。 This is the code I currently use, but it gives me an error of type PersistenceException ("Duplicate annotation"). 这是我当前使用的代码,但是它给我一个PersistenceException类型的错误(“重复注释”)。

What would be the simplest way to solve this? 解决这个问题的最简单方法是什么? Any help would be appreciated. 任何帮助,将不胜感激。

@Root(name = "body", strict = false)
class NextBusAPIResult {
    @Attribute(name = "copyright")
    public String Copyright;

    @Element(name = "predictions", required = false, type = Predictions.class)
    public Predictions Predictions;

    @ElementList(name = "predictions", required = false, inline = true)
    public List<Predictions> PredictionsForMultiStops;

    @Element(name = "route", required = false, type = RouteInfo.class)
    public RouteInfo RouteInfo;

    @ElementList(name = "route", required = false, inline = true)
    public List<RouteSchedule> RouteSchedules;
}

The simplest way to solve this is to use multiple elements always. 解决此问题的最简单方法是始终使用多个元素。 Just remove code related to single elements. 只需删除与单个元素相关的代码即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单XML反序列化具有相同名称的不同元素类型 - Simple XML deserializing different element types with the same name 简单的xml和具有相同名称但名称空间不同的元素 - simple xml and element with same name but different namespace 元素“title”已与@org.simpleframework.xml.ElementList 一起使用 - Element 'title' is already used with @org.simpleframework.xml.ElementList Jackson XML:如何从列表名称中获得不同的元素名称? - Jackson XML: How to have different element names from list name? 具有多个元素名称的反序列化的简单xml框架 - Simple xml framework deserializing with multiple element names XML模式在两种不同的情况下具有两个具有相同名称的元素类型 - XML schema has two element types with same name in two different cases 使用SimpleFramework反序列化多个不同的标签(具有相同的名称) - Deserializing multiple different tags (with the same name) with SimpleFramework 在Jackson中反序列化同名但不同类型的属性? - Deserializing attributes of same name but different types in Jackson? 从Java DOM中的XML获取元素名称 - Get element name from XML in Java DOM 简单的XML框架:序列化时elementlist中的字符串没有名称空间前缀 - Simple XML Framework: Strings in elementlist not having namespace prefix when serializing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM