Concepts / RESTful APIs and Data Exchange

RESTful APIs and Data Exchange

XML is a text-based format that uses custom tags to describe the meaning of structured, hierarchical data.

  • Programming

From Web Pages to Program Documents

The web first became widely associated with displaying documents in browsers. As programs became able to retrieve and parse documents over HTTP, developers recognized that a document could be designed for another program to read rather than for a person to view. XML emerged as one format for this kind of structured data exchange.

Key point

XML is a text-based format that uses custom tags to describe the meaning of structured, hierarchical data.

System ACreates XMLXML documentStructured dataHTTPExchange pathSystem BParses and uses datacreatessent overdelivers to
How does structured XML data move from one program or system to another through an exchange process?

Reading XML as a Tree

An XML document is organized as a tree of nested elements. A root element sits at the top level. It can contain other elements, and those child elements can contain further elements. This hierarchy represents relationships between pieces of data.

libraryroot elementbookid attributetitlebook titleauthorbook authorbookanother bookyearpublication yearISBNbook identifiercontainscontainscontainscontainscontainscontains
What contains what in an XML document, and how do nested custom tags describe the meaning of each piece of data?
Definition

An XML element has a start tag, content, and an end tag. Elements can contain other elements, so the document's nesting expresses how the data is related.

Worked example

Following a Library Record

A library wants to share a catalog containing books, and each book has a title, author, publication year, and ISBN. How can the document's structure communicate those relationships?

  1. 1
    Find the root

    Treat library as the top-level element that represents the complete catalog.

  2. 2
    Find repeated records

    Place book elements inside library. Multiple book elements represent multiple catalog entries.

  3. 3
    Find each book's details

    Place title, author, year, and ISBN inside the relevant book element so the program can associate each value with that book.

  4. 4
    Read the meaning

    The custom element names describe what each piece of content represents, making the document self-describing.

Result

The hierarchy lets a program identify the catalog, its books, and the details belonging to each book.

Meaning Instead of Presentation

HTML and XML both use tags, but their purposes differ. HTML uses a predefined set of tags to describe how information should be displayed in a browser. XML lets the document designer define custom tags that describe what the information means.

HTMLbrowser presentationpredefined tagdisplay purposeXMLdata meaningcustom taginformation purposeusesdefines
What is the difference between tags that describe presentation and tags that describe data meaning?
QuestionHTMLXML
Primary purposeDescribe how data should be displayedDescribe what data means
TagsUses a predefined setUses custom tags defined for the data
Typical document roleDocument displayed in a browserDocument read and processed by programs

The central distinction is presentation versus meaning.

Choosing XML or JSON

XML and JSON are both used to exchange information across the web, but the source distinguishes their common strengths. XML is best suited to document-style data with complex hierarchies. JSON is better suited to simple dictionaries, lists, and lightweight program-to-program communication.

XMLcomplex documenthierarchiesdocument-style datacomplex hierarchyJSONsimple dictionaries andlistslightweight exchangeprogram-to-programwell suited towell suited to
How do XML and JSON differ in structure and intended use for data exchange?
FormatBest suited forReason to consider it
XMLDocument-style data with complex hierarchiesCustom semantic tags and nested elements represent relationships clearly
JSONSimple dictionaries, lists, and lightweight program-to-program communicationIt is suited to simpler data exchange

The choice depends on the shape and purpose of the data.

Best practice

Choose based on the data you need to represent, not merely on the fact that both formats can travel across the web. A complex, document-like hierarchy points toward XML; a simple dictionary or list used in lightweight communication points toward JSON.

How Programs Consume Documents

A program-consumed document is designed so another program can interpret its structure and values. In an XML exchange, one system creates the document, sends it over HTTP, and the receiving system parses it. Parsing means extracting the information from the document so the receiving system can use it.

libraryroot elementbookrecord elementtitlechild elementbook titlecontent valueapplication dataextracted and usedcontainscontainsdescribesparsed into
How does a program interpret the structure and values in a document designed for machine consumption?
Worked example

A Library-to-Application Exchange

A library database needs to provide a book catalog to a web application that displays the books.

  1. 1
    Create

    The library system creates an XML document with a library root, book elements, and child elements for each book's title, author, year, and ISBN.

  2. 2
    Send

    The library system sends the XML document over HTTP to the other system.

  3. 3
    Parse

    The web application parses the XML and extracts the book information.

  4. 4
    Use

    The application can use the extracted information to display the books, store the information in a database, or send it to another system.

Result

The XML document acts as the vehicle carrying understandable book information between systems without requiring the receiving system to know the sender's internal structure.

Mistakes in Format Selection

  • Common mistake

    Treating XML as a presentation language like HTML.

    What it looks like

    Assuming that an XML tag primarily tells a browser how to display content.

    Why it fails

    XML tags describe the meaning of data, while HTML describes how information should be displayed.

    Do this instead

    Read XML tags as semantic labels for data and relationships.

  • Common mistake

    Assuming XML uses only a fixed set of predefined tags.

    What it looks like

    Expecting every XML document to use the same tags for every domain.

    Why it fails

    XML is extensible: the document designer defines custom tags suited to the data.

    Do this instead

    Look at the document's custom element names to understand what its data represents.

  • Common mistake

    Choosing XML or JSON without considering the data shape.

    What it looks like

    Selecting JSON automatically even when the information is document-style data with a complex hierarchy.

    Why it fails

    The formats serve different purposes. XML is suited to complex document hierarchies, while JSON is suited to simple dictionaries, lists, and lightweight communication.

    Do this instead

    Match the format to the structure and purpose of the exchange.

  • Common mistake

    Thinking that sending an XML document completes the exchange.

    What it looks like

    Ignoring the receiving system's need to parse the document.

    Why it fails

    The receiving system must parse the XML to extract and use its information.

    Do this instead

    Trace the complete process: create, send over HTTP, parse, and use.

Practice the Decision

EASY

A system must exchange a catalog containing many records. Each record has several related fields, and the catalog has a natural document-like hierarchy. Would XML or JSON be the better fit according to the source? Explain your choice, then describe the exchange process from the sending system to the receiving system.

Hints
  • First identify whether the data is document-style and hierarchically structured.
  • Then remember which format is associated with complex hierarchies.
  • Include creation, HTTP exchange, parsing, and use in your process description.
  1. 1
    A strong answer chooses XML because the catalog has a complex, document-like hierarchy. One system creates the XML document, sends it over HTTP, and the other system parses it to extract and use the catalog information.

Key Takeaways

  • XML is a text-based format for encoding structured, hierarchical data with custom semantic tags.
  • XML describes what data means, while HTML describes how information should be displayed.
  • XML documents form trees of nested elements, with roots, child elements, content, and end tags representing relationships.
  • Programs exchange XML by creating a document, sending it over HTTP, and parsing it on the receiving system.
  • XML fits complex document-style hierarchies, while JSON fits simple dictionaries, lists, and lightweight program-to-program communication.

Related concepts

More concepts starting with R