Comparing XML and JSON for Data Exchange
An XML element is a complete unit consisting of an opening tag, content, and a closing tag; it can also be represented as a self-closing tag if empty.
Reading XML as Structured Meaning
When data is exchanged between systems, its structure must be understandable to both machines and people. XML organizes information by describing what the data means rather than how it should look on a screen. Its strict structure is built from elements, attributes, and nesting. These building blocks are the XML side of understanding structured data exchange, including comparisons with other formats such as JSON.
To read an XML document, first look for complete elements. Then inspect any attributes attached to their opening tags and the way elements are nested inside one another.
The Three-Part XML Element
An XML element is a complete unit of data. In its standard form, it has three parts: an opening tag, content, and a closing tag. The opening tag begins with a less-than symbol, contains the element name, and ends with a greater-than symbol. The closing tag uses the same element name, but places a forward slash before that name. Everything between the two tags is the element's content.
Identifying the Parts
Examine the XML element <name>Chuck</name>.
Opening tag: The opening tag is <name>. It identifies the beginning of an element named name.
Content: The content is Chuck. It appears between the opening and closing tags.
Closing tag: The closing tag is </name>. The forward slash shows that the name element is being closed.
The complete element contains an opening tag, the text Chuck, and a matching closing tag.
A matching pair of tags defines the boundary of an element. The opening tag starts the element, and the closing tag ends it. The element's content is whatever appears between those boundaries.
Metadata in Opening Tags
An attribute is optional metadata attached to an opening tag. It has a name, an equals sign, and a quoted value, written in the form name="value". Multiple attributes can appear on one opening tag, separated by spaces. An element can exist without attributes.
Separating Metadata from Content
Examine the XML element <phone type="intl">555-0199</phone>.
Read the element name: The element is named phone.
Read the attribute: type="intl" is attached to the opening tag. It describes what kind of phone number the element contains.
Read the content: 555-0199 appears between the tags. It is the actual phone-number content, not metadata about that content.
The attribute identifies a property of the phone element, while the text between the tags is the primary data.
Empty Elements and Self-Closing Tags
An element with no text and no child elements is empty. XML allows an empty element to be written in two equivalent ways. The traditional form uses an opening tag immediately followed by a closing tag, such as <email></email>. The shorter form uses a self-closing tag, such as <email />. Both forms represent the same empty element.
The self-closing form is more compact and is often preferred in practice. An empty element may still have attributes. For example, <email hide="yes" /> is self-closing but also carries the hide="yes" metadata on its opening tag.
What do you think happens?
Which of these represents an empty email element: <email></email>, <email />, or both?
Reveal answer
Answer: Both forms
The paired form has an opening and closing tag with nothing between them. The self-closing form combines those two tags into one tag ending with a forward slash.
Building a Parent-Child Tree
XML elements can be placed inside other elements. This nesting creates a tree-like hierarchy. An element that contains other elements is a parent, and the elements inside it are children. The outermost element containing all the others is the root element. A document has a single root element containing the rest of its elements.
Following the Hierarchy
Consider a person element containing name, phone, and email elements.
Find the root: person is the outermost element, so it is the root.
Find the children: name, phone, and email are directly inside person, so they are its children.
Interpret ownership: Because phone and email are nested inside person, the structure shows that they belong to that person.
Inspect each child: name contains text content, phone contains text content and an attribute, and email is empty but still a valid child.
Nesting represents relationships: person is the root and name, phone, and email are related child elements.
The hierarchy is more than visual indentation. It tells you which data belongs together. In the source example, phone and email belong to person because they are nested inside person rather than placed beside it as unrelated top-level data.
Choosing Attributes or Child Elements
| Use | Best suited for | Source example |
|---|---|---|
| Attributes | Metadata, properties, or classifications describing an element | type="intl" |
| Nested elements | Primary content or data that may have its own structure or attributes | <phone>555-0199</phone> |
Suppose an element stores a phone number. The number itself is primary content, so it belongs between the opening and closing tags. A classification such as whether the number is international describes the phone element, so it belongs in an attribute. This distinction helps keep an XML document understandable and predictable.
Mistakes When Reading XML
Treating the opening tag as the entire element
The opening tag only begins the element. A complete nonempty element also needs content and a matching closing tag.
Fix:
Read the full unit, such as <name>Chuck</name>.Confusing an attribute with the element's main content
type="intl" describes the phone element, while 555-0199 is the primary content.
Fix:
Classify information attached to the opening tag as metadata and information between the tags as content.Assuming an empty element is invalid
A self-closing tag is a valid representation of an element with no text or child elements.
Fix:
Recognize both <email></email> and <email /> as valid empty-element forms.Ignoring nesting when interpreting relationships
Nesting creates parent-child relationships in the XML tree.
Fix:
Identify the containing element as the parent and the elements inside it as children.Forgetting that attributes are optional
Attributes provide optional metadata; an element can have no attributes.
Fix:
Look for attributes, but do not expect them on every element.
Practice the XML Trace
Analyze the following generated XML structure: <account status="active"><owner>Rina</owner><note /></account>. Identify the root element, the attribute, the text-containing child, and the self-closing child. Then explain which information is metadata and which information is primary content.
Hints
- The root is the outermost element containing all the others.
- An attribute appears inside an opening tag in name="value" form.
- A self-closing tag represents an empty element.
- An XML element normally consists of an opening tag, content, and a closing tag.
- Attributes are optional name="value" metadata attached to opening tags.
- An empty element can use paired empty tags or a self-closing tag.
- Nested elements form a parent-child hierarchy with one root element containing the document's other elements.
- Attributes describe elements, while nested elements hold primary content or further structure.
Key Takeaways
- XML represents meaningful data through elements, attributes, and nesting.
- A complete nonempty element has an opening tag, content, and a matching closing tag.
- Attributes attach to opening tags and describe properties or classifications of an element.
- Self-closing tags provide a concise form for empty elements.
- Nesting creates a hierarchy in which a root element contains parent and child relationships.