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

时间:2016-01-28 19:59:38

标签: java android xml retrofit simple-framework

我有一个需要解析的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解析器。

有关我做错的任何建议吗?

先谢谢你。

1 个答案:

答案 0 :(得分:4)

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

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

最后,Vehicle类看起来像这样:

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(){}

}