Concepts / RESTful APIs and Data Exchange

RESTful APIs and Data Exchange

XML (eXtensible Markup Language) is a text-based format for encoding structured, hierarchical data using custom tags that describe the meaning of the 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.

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

createssent overdelivers toSystem ACreates XMLXML documentStructured dataHTTPExchange pathSystem BParses and uses data
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.

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

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.

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?

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

Find repeated records: Place book elements inside library. Multiple book elements represent multiple catalog entries.

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.

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

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.

usesdefinesHTMLbrowser presentationpredefined tagdisplay purposeXMLdata meaningcustom taginformation purpose
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.

well suited towell suited toXMLcomplex documenthierarchiesdocument-style datacomplex hierarchyJSONsimple dictionaries andlistslightweight exchangeprogram-to-program
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

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.

containscontainsdescribesparsed intolibraryroot elementbookrecord elementtitlechild elementbook titlecontent valueapplication dataextracted and used
How does a program interpret the structure and values in a document designed for machine consumption?

A Library-to-Application Exchange

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

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.

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

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

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

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

  • Treating XML as a presentation language like HTML.

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

    Fix: Read XML tags as semantic labels for data and relationships.

  • Assuming XML uses only a fixed set of predefined tags.

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

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

  • Choosing XML or JSON without considering the data shape.

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

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

  • Thinking that sending an XML document completes the exchange.

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

    Fix: 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. 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.