带有 Retrofit 1.9 的 SimpleXML,"属性'版本'在类中不匹配"



我有一个需要解析的XML(我无法控制XML或其格式(:

<?xml version="1.0" encoding="UTF-8"?>
<Siri xmlns="http://www.siri.org.uk/siri" version="1.3">
<ResponseTimestamp>2016-01-27T21:49:18.6841619-07:00</ResponseTimestamp>
<VehicleMonitoringDelivery version="1.3">
  <ResponseTimestamp>2016-01-27T21:49:18.6841619-07:00</ResponseTimestamp>
  <ValidUntil>2016-01-27T21:49:28.6841619-07:00</ValidUntil>
  <VehicleActivity>
     <RecordedAtTime>2016-01-27T21:49:18.6841619-07:00</RecordedAtTime>
     <MonitoredVehicleJourney>
        <LineRef>750</LineRef>
        <DirectionRef>SOUTHBOUND</DirectionRef>
        <FramedVehicleJourneyRef>
           <DataFrameRef>2016-01-27T00:00:00-07:00</DataFrameRef>
           <DatedVehicleJourneyRef>2519014</DatedVehicleJourneyRef>
        </FramedVehicleJourneyRef>
        <PublishedLineName>FRONTRUNNER</PublishedLineName>
        <OriginRef>601084</OriginRef>
        <DestinationRef>801164</DestinationRef>
        <Monitored>True</Monitored>
        <VehicleLocation>
           <Longitude>-111.86847</Longitude>
           <Latitude>40.401028</Latitude>
        </VehicleLocation>
        <ProgressRate>1</ProgressRate>
        <CourseOfJourneyRef>18127</CourseOfJourneyRef>
        <VehicleRef>101</VehicleRef>
        <MonitoredCall>
           <StopPointRef>801160</StopPointRef>
           <VisitNumber>1</VisitNumber>
           <VehicleAtStop>false</VehicleAtStop>
        </MonitoredCall>
        <OnwardCalls>
           <OnwardCall>
              <StopPointRef>23076</StopPointRef>
              <VisitNumber>1</VisitNumber>
              <StopPointName>OREM CENTRAL STATION</StopPointName>
           </OnwardCall>
        </OnwardCalls>
        <Extensions>
           <LastGPSFix>2016-01-27T21:49:09.473</LastGPSFix>
           <Scheduled>False</Scheduled>
           <Bearing>137.91188191173691</Bearing>
           <Speed>76.7898894465909</Speed>
           <DestinationName>Provo</DestinationName>
        </Extensions>
     </MonitoredVehicleJourney>
  </VehicleActivity>
  </VehicleMonitoringDelivery>
</Siri>

我有一个超级基本和精简的类车辆,只有根元素,像这样:

import org.simpleframework.xml.Root;
@Root(name="VehicleActivity")
 public class Vehicle
{
    public Vehicle(){}
}

在 xml 之外,我只关心 VehicleActivity 标签内的数据。

当我尝试解析它时,从改造回调的成功方法中,我得到错误Attribute 'version' does not have a match in class

01-28 12:49:48.561 30643-30643 E/MY_APP: org.simpleframework.xml.core.AttributeException: Attribute 'version' does not have a match in class com.eduardoflores.utarider.model.Vehicle at line 1

我知道如何使用JSON执行此操作,但这是我第一次尝试使用XML解析器执行此操作。

关于我做错了什么有什么建议吗?

提前谢谢你。

我想出了这个问题,并决定在这里发布修复程序,因为那个可怜的灵魂将来会在某个地方遇到同样的问题。

我必须更改根,修改 Vehicle 类以包含我想要的路径,以及返回MonitoredVehicleJourney对象列表的@ElementList注释。

最后,车辆类如下所示:

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;
import java.util.List;

/**
 * @author Eduardo Flores
 */
@Root(name = "Siri", strict=false)
public class Vehicle
{
    @Path("VehicleMonitoringDelivery/VehicleActivity")
    @ElementList(entry = "MonitoredVehicleJourney", inline = true)
    public List<MonitoredVehicleJourney> monitoredVehicleJourney;
    public Vehicle(){}
}

最新更新