Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleXML with Retrofit 1.9, 'Attribute 'version' does not have a match in class'

I have an XML I need to parse (I don't have control over the XML or its format):

<?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>

I have a super basic and stripped down class Vehicle with nothing but the root element, like this:

import org.simpleframework.xml.Root;

@Root(name="VehicleActivity")
 public class Vehicle
{
    public Vehicle(){}
}

Out of the xml, I only care about the data inside the VehicleActivity tag.

When I try to parse it, from the success method of the retrofit callback, I get the error 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

I know how to do this with JSON but this is my first attempt to do it with an XML parser.

Any suggestions on what I'm doing wrong?

Thank you in advanced.

like image 642
TooManyEduardos Avatar asked Jan 28 '16 19:01

TooManyEduardos


1 Answers

I figured out this issue and decided to post the fix here, for that one poor soul who will have this same issue somewhere in the future.

I had to change the root, modify the Vehicle class to include the path I wanted, and the @ElementList annotation to return a list of MonitoredVehicleJourney objects.

At the end, the Vehicle class looks like this:

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

}
like image 52
TooManyEduardos Avatar answered Nov 05 '22 19:11

TooManyEduardos