Concepts / XML: Structuring Complex Data

XML: Structuring Complex Data

The data carried in the cycle is represented using XML or JSON, formats that both applications can understand and parse.

  • Programming

From Isolated Programs to Connected Services

Modern software rarely works in isolation. A weather application may need data from a meteorological service, while a payment processor may need to verify a transaction with a bank. In each case, one application needs a controlled way to use a capability provided by another application.

An application programming interface, or API, provides that controlled connection. It is an application-to-application contract: a published set of rules describing how one program can access services provided by another program. The contract lets applications exchange requests and responses without requiring the consumer to understand the provider's internal implementation.

Consumer applicationRequests a servicePublished APIRules for accessProvider applicationProvides a serviceResponse dataRequested data orconfirmationfollows rulesrequestresponsereturns through HTTP
How does a request travel from one application to another, and how does the service provider send data back?

The Three Parts of an API Contract

Every API involves three essential elements. The service provider is the application offering a service. The API specification is the published set of rules. The service consumer is the application using that service. The consumer reads the specification and creates requests that follow its rules.

Service providerOffers servicesParametersAccepted inputsAPI specificationPublished rulesData formatsRequest and responseformatsService consumerUses servicespublishesguidesdefinesdefines
What rules connect a consuming application to a service provided by another program?
In the real world

The source describes an API using a restaurant menu as an analogy. The restaurant represents the service provider, the menu represents the API, and the customer represents the service consumer. The menu states what is available and how to order, while the kitchen's internal operations remain hidden.

Following One API Request

What do you think happens?

A consumer application needs a service from another application. What happens first in the API cycle?

  • The consumer accesses the provider's internal database directly

  • The consumer sends a request that follows the API's rules

  • The provider sends all of its internal data to the consumer

  • The consumer changes how the provider stores its data

Reveal answer
Answer:

The consumer sends a request that follows the API's rules.

The consumer specifies the service it wants and provides any necessary parameters. The provider then processes the request according to the API rules and sends back requested data or confirmation of an action.

  1. 1
    The consumer application identifies the service it wants.
  2. 2
    The consumer sends a request with any necessary parameters.
  3. 3
    The provider application receives and processes the request according to the API rules.
  4. 4
    The provider sends a response containing requested data or confirmation of an action.
  5. 5
    The consumer uses the response without needing to know how the provider stores or processes its data internally.

This cycle takes place over HTTP, which acts as the transport protocol. The data carried in the cycle is represented using XML or JSON, formats that both applications can understand and parse. HTTP answers how the exchange travels; XML or JSON answers how the exchanged data is represented.

XML Inside the Exchange

XML is one of the data representation formats an API can use when applications exchange information. Its role is to represent the data in a form that both the consumer and provider can understand and parse. The API contract specifies the expected format, so the consumer can interpret the provider's response without learning the provider's internal storage system.

Service responseStructured dataWeather informationRelated valuesCurrent temperatureA requested valueForecastAnother service resultcontainscontainscontains
What contains what in an XML representation of related service data?

The hierarchy illustrates the organizing idea: a response can contain a larger piece of information, which can contain related pieces of information. For example, a weather service may expose separate services for the current temperature, a forecast, and historical data. The exact representation rules come from the API specification; the consumer should use those rules rather than assume how the provider organizes its internal data.

Transport and Representation

Consumer applicationRequests or receivesHTTPTransport protocolXMLData representationJSONData representationProvider applicationProcesses the exchangeusescarries exchangecan represent data carried bycan represent data carried by
How do HTTP, XML, and JSON differ, and where does each fit when applications exchange data?
TechnologyRole in the API exchange
HTTPTransports the request and response between applications
XMLRepresents exchanged data in a format applications can understand and parse
JSONProvides another data representation format for exchanged data

HTTP concerns transport; XML and JSON concern data representation.

These technologies have different jobs. HTTP carries the communication between applications. XML and JSON represent the data being exchanged. The API specification establishes which services are available, what parameters are accepted, and what request and response formats the consumer should use.

Abstraction at the API Boundary

Worked example

Requesting a Weather Service

Illustrate how a consumer can use a weather service through an API without learning the provider's internal implementation.

  1. 1
    Choose the service

    The consumer application needs the current temperature, so it selects the provider's service for that purpose.

  2. 2
    Follow the contract

    The consumer reads the API specification to determine the required parameters and the expected request and response formats.

  3. 3
    Send the request

    The consumer sends its request to the provider over HTTP.

  4. 4
    Receive represented data

    The provider processes the request and returns the requested data in the format specified by the API, such as XML or JSON.

  5. 5
    Use the result

    The consumer uses the response without needing to know how the provider collects, stores, processes, or analyzes weather data.

Result

The API boundary exposes a specific weather capability while hiding the provider's internal complexity.

This abstraction protects the provider's internal systems because the consumer can access only the functionality explicitly exposed through the API. It also allows the provider to change its internal implementation without breaking consumers, provided the API contract remains the same. At the same time, the consumer's code is simpler because it only needs to learn the published API.

Mistakes About XML and APIs

  • Common mistake

    Treating XML as the transport mechanism

    What it looks like

    Saying that XML carries the request between applications

    Why it fails

    HTTP is the transport protocol in the API exchange. XML is a data representation format.

    Do this instead

    Describe HTTP as the transport and XML as one possible format for representing the exchanged data.

  • Common mistake

    Assuming the consumer needs access to the provider's internal system

    What it looks like

    Expecting a weather application to understand the provider's storage or processing code

    Why it fails

    The API hides internal complexity and exposes only specified services.

    Do this instead

    Focus on the published services, parameters, and request and response formats.

  • Common mistake

    Describing an API as only a piece of software

    What it looks like

    Ignoring the rules that tell consumers what services and formats are available

    Why it fails

    The API is an application-to-application contract involving a provider, a published specification, and a consumer.

    Do this instead

    Include the published rules and the applications on both sides of the exchange.

  • Common mistake

    Assuming every API uses XML

    What it looks like

    Treating XML as the only possible representation format

    Why it fails

    The source identifies XML and JSON as data representation formats used in API communication.

    Do this instead

    Say that an API may use XML or JSON according to its published specification.

Check Your Understanding

MEDIUM

A banking application needs a transaction verification service from another application. Explain the roles of the service provider, API specification, and service consumer. Then identify which technology transports the exchange and which formats may represent the exchanged data.

Hints
  • Start with the application offering transaction verification.
  • Separate the published rules from the application using the service.
  • Remember that transport and representation have different roles.
  1. 1
    An API is an application-to-application contract that publishes rules for accessing services. The consumer sends a request, the provider processes it, and the provider returns data or confirmation according to the contract. HTTP transports the exchange, while XML or JSON represents the data. XML can organize complex exchanged information in a structured hierarchy, but the consumer still relies on the API specification rather than the provider's internal implementation.

Key Takeaways

  • An API is an application-to-application contract containing published rules for accessing services.
  • The three participants are the service provider, the API specification, and the service consumer.
  • An API exchange follows a request-and-response cycle over HTTP.
  • XML and JSON represent data exchanged through APIs; HTTP transports that exchange.
  • APIs provide abstraction by exposing selected services while hiding internal complexity.

More concepts starting with X