0

I would like to begin by saying that I am very new to Android development and especially using Retrofit for HTTPS requests.

I am having a hard time creating my data classes to represent the data I want to receive from a transit API. What makes it more difficult for me, as a beginner, is that almost all Youtube tutorials show how to get requests which come in JSON format. However the API I am using returns data in XML. I am trying to use SimpleXML as a converter for my XML file.

It would be much appreciated if someone would be able to help me understand what my data classes should look like and why they should look that way.

Below is one example of a potential XML document that could be returned upon and HTML request to this API.

I would be glad to give more information if it may be beneficial. Thank you!

<Buses xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Bus>
        <VehicleNo>15010</VehicleNo>
        <TripId>13479416</TripId>
        <RouteNo>099</RouteNo>
        <Direction>EAST</Direction>
        <Destination>COMM'L-BDWAY STN</Destination>
        <Pattern>E1</Pattern>
        <Latitude>49.263350</Latitude>
        <Longitude>-123.128267</Longitude>
        <RecordedTime>04:47:37 pm</RecordedTime>
        <RouteMap>
            <Href>https://nb.translink.ca/geodata/099.kmz</Href>
        </RouteMap>
    </Bus>
    <Bus>
        <VehicleNo>15012</VehicleNo>
        <TripId>13479306</TripId>
        <RouteNo>099</RouteNo>
        <Direction>EAST</Direction>
        <Destination>COMM'L-BDWAY STN</Destination>
        <Pattern>E1</Pattern>
        <Latitude>49.262333</Latitude>
        <Longitude>-123.070167</Longitude>
        <RecordedTime>04:47:03 pm</RecordedTime>
        <RouteMap>
            <Href>https://nb.translink.ca/geodata/099.kmz</Href>
        </RouteMap>
    </Bus>
    <Bus>
        <VehicleNo>15014</VehicleNo>
        <TripId>13479455</TripId>
        <RouteNo>099</RouteNo>
        <Direction>EAST</Direction>
        <Destination>COMM'L-BDWAY STN</Destination>
        <Pattern>E1</Pattern>
        <Latitude>49.266683</Latitude>
        <Longitude>-123.243167</Longitude>
        <RecordedTime>04:47:52 pm</RecordedTime>
        <RouteMap>
            <Href>https://nb.translink.ca/geodata/099.kmz</Href>
        </RouteMap>
    </Bus>
</Buses>

I tried creating 3 classes: a class to represent a class which has one field (a Bus list), a Bus class which represents Direction, Destination, TripId, etc., and finally a RouteMap class which has one field (the Href, which I declared as a string.)

yoozr999
  • 1
  • 1
  • You can use: [XML to JSON converter](https://codebeautify.org/xmltojson) to convert your XML example to JSON. Then use [JSON to Kotlin](https://transform.tools/json-to-kotlin) to generate the data classes. Since SimpleXMLConverter is deprecated, you can follow [these instructions](https://stackoverflow.com/a/54009923/1827254) and use [Tickaroo](https://github.com/Tickaroo/tikxml) (but it seems old as well) together with Retrofit to directly map your XML response into the data classes. – Eselfar Jul 05 '23 at 13:08

0 Answers0