Fetching XML within Android program
I am currently developing an android program. After some searching i can't seem to find out a solution to fetch or request using XML within my android program.
I'm still at the early stages of the program. I can show the xml code i will be using. It's basically requesting data using a username and password. I want to use this data so that the user can for example in a text/input box use the MonitoringRef identifier and receive the data through the XML request and have it presented within the app.
A request would consist of:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Siri version="1.0" xmlns="http://www.siri.org.uk/">
<ServiceRequest>
<RequestTimestamp>2007-01-11T10:38:12Z</RequestTimestamp>
<RequestorRef>CLIENT_APP_ID</RequestorRef>
<StopMonitoringRequest version="1.0">
<RequestTimestamp>2007-01-11T10:38:12Z</RequestTimestamp>
<MessageIdentifier>67890</MessageIdentifier>
<MonitoringRef>260057046</MonitoringRef>
</StopMonitoringRequest>
</ServiceRequest>
</Siri>
A response would consist of:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
10 - 11
Kizoom Traveline API at 03/09/2010
<Siri version="1.0" xmlns="http://www.siri.org.uk/">
<ServiceDelivery>
<ResponseTimestamp>2007-05-22T14:39:04+01:00</ResponseTimestamp>
<StopMonitoringDelivery version="1.0">
<ResponseTimestamp>2007-05-
22T14:39:04+01:00</ResponseTimestamp>
<RequestMessageRef>12345</RequestMessageRef>
<MonitoredStopVisit>
<RecordedAtTime>2007-05-22T14:39:03+01:00</RecordedAtTime>
<MonitoringRef>leiadadg</MonitoringRef>
<MonitoredVehicleJourney>
<FramedVehicleJourneyRef>
<DataFrameRef>-</DataFrameRef>
<DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
</FramedVehicleJourneyRef>
<VehicleMode>bus</VehicleMode>
<PublishedLineName>22</PublishedLineName>
<DirectionName>Norwich: Rail Station
Forecourt</DirectionName>
<MonitoredCall>
<AimedDepartureTime>2007-05-
22T14:53:00+01:00</AimedDepartureTime>
<ExpectedDepartureTime>2007-05-
22T14:55:00+01:00</ExpectedDepartureTime>
</MonitoredCall>
</MonitoredVehicleJourney>
</MonitoredStopVisit>
</StopMonitoringDelivery>
<StopMonitoringDelivery version="1.0">
<ResponseTimestamp>2007-05-
22T14:39:04+01:00</ResponseTimestamp>
<RequestMessageRef>67890</RequestMessageRef>
<MonitoredStopVisit>
<RecordedAtTime>2007-05-22T14:39:03+01:00</RecordedAtTime>
<MonitoringRef>260057046</MonitoringRef>
<MonitoredVehicleJourney>
<FramedVehicleJourneyRef>
<DataFrameRef>-</DataFrameRef>
<DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>开发者_StackOverflow
</FramedVehicleJourneyRef>
<VehicleMode>bus</VehicleMode>
<PublishedLineName>3</PublishedLineName>
<DirectionName>Norwich: Ber Street John
Lewis</DirectionName>
<MonitoredCall>
<AimedDepartureTime>2007-05-
22T14:53:00+01:00</AimedDepartureTime>
<ExpectedDepartureTime>2007-05-
22T14:57:00+01:00</ExpectedDepartureTime>
</MonitoredCall>
</MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
<RecordedAtTime>2007-05-22T14:39:03+01:00</RecordedAtTime>
<MonitoringRef>260057046</MonitoringRef>
<MonitoredVehicleJourney>
<FramedVehicleJourneyRef>
<DataFrameRef>-</DataFrameRef>
<DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
</FramedVehicleJourneyRef>
<VehicleMode>bus</VehicleMode>
<PublishedLineName>18</PublishedLineName>
<DirectionName>Old Catton: White Woman Lane/Proctor
Rd</DirectionName>
<MonitoredCall>
<AimedDepartureTime>2007-05-
22T14:57:00+01:00</AimedDepartureTime>
</MonitoredCall>
</MonitoredVehicleJourney>
</MonitoredStopVisit>
</StopMonitoringDelivery>
</ServiceDelivery>
</Siri>
Element in XML:
- StopMonitoringDelivery –present for each StopMonitoringRequest in the request
- RequestMessageRef – corresponds to MessageIdentifier in the request
- MonitoringRef – indicates which bus stop this StopMonitoringDelivery relates to
- MonitoredStopVisit – represents a single bus departure
- PublishedLineName –service name
- DirectionName – destination
- AimedDepartureTime –scheduled departure time
- ExpectedDepartureTime –estimated departure time
I'm just wondering if anyone can provide me to resources or a solution to provide this within the app.
You can interact with remote service via HTTP protocol using DefaultHttpClient and HttpPost / HttpGet classes.
To parse an XML document from string response you can use DocumentBuilder.parse method It will provide to you an implementation of W3C Document Object Model to work with.
Also, you'll need to use some serialization facility to prepare a string representation of you XML document (to send as a response or else). The solution is depends on minimal SDK version of your application. With API level 8 you can use LSSerializer to do so.
精彩评论