RESTful APIs and Data Exchange
XML is a text-based format that uses custom tags to describe the meaning of structured, hierarchical 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?
- 1Find the root
Treat library as the top-level element that represents the complete catalog.
- 2Find repeated records
Place book elements inside library. Multiple book elements represent multiple catalog entries.
- 3Find 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.
- 4Read 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 |
The choice depends on the shape and purpose of the data.
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.
- 1Create
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.
- 2Send
The library system sends the XML document over HTTP to the other system.
- 3Parse
The web application parses the XML and extracts the book information.
- 4Use
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
Common mistake Treating XML as a presentation language like HTML.
What it looks likeAssuming that an XML tag primarily tells a browser how to display content.
Why it failsXML tags describe the meaning of data, while HTML describes how information should be displayed.
Do this insteadRead XML tags as semantic labels for data and relationships.
Common mistake Assuming XML uses only a fixed set of predefined tags.
What it looks likeExpecting every XML document to use the same tags for every domain.
Why it failsXML is extensible: the document designer defines custom tags suited to the data.
Do this insteadLook 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 likeSelecting JSON automatically even when the information is document-style data with a complex hierarchy.
Why it failsThe formats serve different purposes. XML is suited to complex document hierarchies, while JSON is suited to simple dictionaries, lists, and lightweight communication.
Do this insteadMatch the format to the structure and purpose of the exchange.
Common mistake Thinking that sending an XML document completes the exchange.
What it looks likeIgnoring the receiving system's need to parse the document.
Why it failsThe receiving system must parse the XML to extract and use its information.
Do this insteadTrace 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.
- 1A 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.