java - SimpleXML with Retrofit 1.9, 'Attribute ' 版本'在类中没有匹配项'

标签 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>

我有一个 super 基本的精简类 Vehicle,除了根元素什么都没有,像这样:

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 类以包含我想要的路径,以及 @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(){}

}

关于java - SimpleXML with Retrofit 1.9, 'Attribute ' 版本'在类中没有匹配项',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35070882/

相关文章:

java - 如何在java中运行pentaho pig作业

Java 8 HashMap 内部结构

android - Android 中的 "activity record object"是什么?

php - Android PHP 网络服务安全

android - 当手动将进度设置为 MotionLayout 时,它会清除所有约束

java - AWT 按钮在 VLCJ 中不可点击

java - 在 spring boot 中创建自定义查询时出错

java - 无法实例化类-android编程

java - 如何自定义 jaxb 生成?

python - 无法使用默认命名空间写入 XML 文件