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.
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.
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.
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.
| Question | HTML | XML |
|---|---|---|
| Primary purpose | Describe how data should be displayed | Describe what data means |
| Tags | Uses a predefined set | Uses custom tags defined for the data |
| Typical document role | Document displayed in a browser | Document 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.
| Format | Best suited for | Reason to consider it |
|---|---|---|
| XML | Document-style data with complex hierarchies | Custom semantic tags and nested elements represent relationships clearly |
| JSON | Simple dictionaries, lists, and lightweight program-to-program communication | It 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.
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
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.
- 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.