Parse xml

49 views
Skip to first unread message

11 Planner

unread,
Apr 30, 2022, 2:35:12 PM4/30/22
to Google Apps Script Community
Hello



<ServiceDelivery>
<ResponseTimestamp>2022-04-30T18:21:55.809458+00:00</ResponseTimestamp>
<ProducerRef>ItoWorld</ProducerRef>
<VehicleMonitoringDelivery>
<ResponseTimestamp>2022-04-30T18:21:55.809458+00:00</ResponseTimestamp>
<RequestMessageRef>6edd7243-49a9-4ad8-87b7-ced18e996689</RequestMessageRef>
<ValidUntil>2022-04-30T18:26:55.809458+00:00</ValidUntil>
<ShortestPossibleCycle>PT5S</ShortestPossibleCycle>
<VehicleActivity>
<RecordedAtTime>2022-04-30T18:21:43+00:00</RecordedAtTime>
<ItemIdentifier>89cde6af-08d0-42ce-8878-f04d61597606</ItemIdentifier>
<ValidUntilTime>2022-04-30T18:26:55.809617</ValidUntilTime>
<MonitoredVehicleJourney>
<LineRef>687</LineRef>
<DirectionRef>outbound</DirectionRef>
<FramedVehicleJourneyRef>
<DataFrameRef>2022-04-30</DataFrameRef>
<DatedVehicleJourneyRef>9800</DatedVehicleJourneyRef>
</FramedVehicleJourneyRef>
<PublishedLineName>687</PublishedLineName>
<OperatorRef>GNEL</OperatorRef>
<DestinationRef>3100Y93963A</DestinationRef>
<VehicleLocation>
<Longitude>-2.143846</Longitude>
<Latitude>54.983175</Latitude>
</VehicleLocation>
<Bearing>199.0</Bearing>
<Occupancy>seatsAvailable</Occupancy>
<BlockRef>151S</BlockRef>
<VehicleRef>670</VehicleRef>
</MonitoredVehicleJourney>
<Extensions>
<VehicleJourney>
<Operational>
<TicketMachine>
<TicketMachineServiceCode>687</TicketMachineServiceCode>
<JourneyCode>1912</JourneyCode>
</TicketMachine>
</Operational>
<VehicleUniqueId>670</VehicleUniqueId>
<DriverRef>99031</DriverRef>
<SeatedOccupancy>0</SeatedOccupancy>
<SeatedCapacity>29</SeatedCapacity>
<WheelchairOccupancy>0</WheelchairOccupancy>
<WheelchairCapacity>1</WheelchairCapacity>
<OccupancyThresholds>40,50</OccupancyThresholds>
</VehicleJourney>
</Extensions>
</VehicleActivity>
</VehicleMonitoringDelivery>
</ServiceDelivery>
</Siri>


Here is my code its only returning the child

function  Parse() {

var url = 'https://data.bus-data.dft.gov.uk/api/v1/datafeed/?operatorRef=GNEL&vehicleRef=670&api_key=[My api key]';
var res = UrlFetchApp.fetch(url).getContentText();
var xml = XmlService.parse(res);
var root = xml.getRootElement();

var elements = root.getChildren();

Logger.log(elements)
 
}

This is the logs

[22-04-30 19:32:20:626 BST] [[Element: <ServiceDelivery [Namespace: http://www.siri.org.uk/siri]/>]]

I need the VehicleRef but all I can get is the response timestamp and producer.

Does anyone know how to do this?

Thanks

Tanaike

unread,
Apr 30, 2022, 8:42:33 PM4/30/22
to Google Apps Script Community
Although I'm not sure whether I could correctly understand your goal, about "I need the VehicleRef", if you want to retrieve "670", how about directly retrieving it from your URL? If my understanding of your goal is not correct, can you provide the sample output value you expect using your showing sample XML data?

Reply all
Reply to author
Forward
0 new messages