Concepts / Comparing XML and JSON for Data Exchange

Comparing XML and JSON for Data Exchange

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.

  • Programming

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.

Key point

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.

<name>opening tagChuckcontent</name>closing tagcontainsends with
How do the opening tag, content, and closing tag combine to form one complete XML element?
Worked example

Identifying the Parts

Examine the XML element <name>Chuck</name>.

  1. 1
    Opening tag

    The opening tag is <name>. It identifies the beginning of an element named name.

  2. 2
    Content

    The content is Chuck. It appears between the opening and closing tags.

  3. 3
    Closing tag

    The closing tag is </name>. The forward slash shows that the name element is being closed.

Result

The complete element contains an opening tag, the text Chuck, and a matching closing tag.

Definition

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.

<phoneelement nametype="intl"metadata>tag endhasprecedes
How is an attribute attached to an opening tag, and how does it describe the element?
Worked example

Separating Metadata from Content

Examine the XML element <phone type="intl">555-0199</phone>.

  1. 1
    Read the element name

    The element is named phone.

  2. 2
    Read the attribute

    type="intl" is attached to the opening tag. It describes what kind of phone number the element contains.

  3. 3
    Read the content

    555-0199 appears between the tags. It is the actual phone-number content, not metadata about that content.

Result

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.

<email></email>paired empty tags<email />self-closing tagempty email elementno text or childrenmeansmeans
What changes in XML syntax when an element contains no content?

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?

  • <email></email> only

  • <email /> only

  • Both forms

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.

personrootnameChuckphone555-0199emailempty elementchildchildchild
How are parent and child elements arranged inside one another to represent hierarchical data?
Worked example

Following the Hierarchy

Consider a person element containing name, phone, and email elements.

  1. 1
    Find the root

    person is the outermost element, so it is the root.

  2. 2
    Find the children

    name, phone, and email are directly inside person, so they are its children.

  3. 3
    Interpret ownership

    Because phone and email are nested inside person, the structure shows that they belong to that person.

  4. 4
    Inspect each child

    name contains text content, phone contains text content and an attribute, and email is empty but still a valid child.

Result

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

UseBest suited forSource example
AttributesMetadata, properties, or classifications describing an elementtype="intl"
Nested elementsPrimary content or data that may have its own structure or attributes<phone>555-0199</phone>

Attributes and nested elements can both carry information, but they serve different semantic roles.

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

  • Common mistake

    Treating the opening tag as the entire element

    What it looks like
    <name>
    Why it fails

    The opening tag only begins the element. A complete nonempty element also needs content and a matching closing tag.

    Do this instead

    Read the full unit, such as <name>Chuck</name>.

  • Common mistake

    Confusing an attribute with the element's main content

    What it looks like
    <phone type="intl">555-0199</phone>
    Why it fails

    type="intl" describes the phone element, while 555-0199 is the primary content.

    Do this instead

    Classify information attached to the opening tag as metadata and information between the tags as content.

  • Common mistake

    Assuming an empty element is invalid

    What it looks like
    <email />
    Why it fails

    A self-closing tag is a valid representation of an element with no text or child elements.

    Do this instead

    Recognize both <email></email> and <email /> as valid empty-element forms.

  • Common mistake

    Ignoring nesting when interpreting relationships

    What it looks like

    Reading phone as unrelated to person even though it is inside person

    Why it fails

    Nesting creates parent-child relationships in the XML tree.

    Do this instead

    Identify the containing element as the parent and the elements inside it as children.

  • Common mistake

    Forgetting that attributes are optional

    What it looks like

    Assuming every opening tag must contain name="value"

    Why it fails

    Attributes provide optional metadata; an element can have no attributes.

    Do this instead

    Look for attributes, but do not expect them on every element.

Practice the XML Trace

EASY

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.
  1. 1
    An XML element normally consists of an opening tag, content, and a closing tag.
  2. 2
    Attributes are optional name="value" metadata attached to opening tags.
  3. 3
    An empty element can use paired empty tags or a self-closing tag.
  4. 4
    Nested elements form a parent-child hierarchy with one root element containing the document's other elements.
  5. 5
    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.

More concepts starting with C