XML Elements

Elements are the fundamental building blocks of any XML document. Every piece of data in XML is wrapped inside an element. Understanding how elements work — how they are named, nested, and structured — is essential to writing correct and meaningful XML.

What is an XML Element?

An XML element consists of a start tag, some content, and an end tag. Together, these three parts form one complete element.

<tagname>content goes here</tagname>
  • Start tag: <tagname> — marks the beginning of the element.
  • Content: The data or other elements placed between the start and end tags.
  • End tag: </tagname> — marks the end of the element. Note the forward slash / before the tag name.

Example

<city>Paris</city>

Here, city is the element name, and Paris is the content of that element.

Types of Element Content

An XML element can contain different types of content:

1. Text Content

The element contains plain text between its tags.

<country>Germany</country>

2. Child Elements (Nested Elements)

An element can contain other elements. The inner elements are called child elements, and the outer element is called the parent element.

<address>
  <street>123 Maple Avenue</street>
  <city>Chicago</city>
  <zip>60601</zip>
</address>

Here, <address> is the parent and <street>, <city>, and <zip> are its children.

3. Mixed Content

An element can contain both text and child elements at the same time. This is called mixed content and is common in document-style XML.

<description>Visit <place>Rome</place> in summer.</description>

4. Empty Elements

Some elements have no content. These are called empty elements and can be written as a self-closing tag.

<separator />

This is equivalent to:

<separator></separator>

Element Naming Rules

Choosing element names correctly is important. XML element names must follow these rules:

  • Names can contain letters, numbers, hyphens, underscores, and periods.
  • Names must start with a letter or underscore — not a number or special character.
  • Names cannot contain spaces.
  • Names cannot start with the letters xml (in any combination of uppercase and lowercase) — that prefix is reserved.
  • Names are case-sensitive: <Product> and <product> are different elements.
Valid NamesInvalid NamesReason Invalid
product1productStarts with a number
first_namefirst nameContains a space
phone-numberxml-dataStarts with "xml"
item.price@priceStarts with special character

Parent, Child, and Sibling Elements

XML documents form a tree-like structure. Understanding the relationship between elements makes it easier to read and write XML.

  • Parent element: An element that contains one or more other elements.
  • Child element: An element that is directly inside a parent element.
  • Sibling elements: Elements that share the same parent.
  • Root element: The top-level element that contains all others.

Example Showing Relationships

<?xml version="1.0" encoding="UTF-8"?>
<school>
  <student>
    <name>Liam Johnson</name>
    <grade>A</grade>
    <age>15</age>
  </student>
  <student>
    <name>Sophia Brown</name>
    <grade>B</grade>
    <age>16</age>
  </student>
</school>
  • <school> is the root element.
  • Each <student> is a child of <school> and a sibling to the other <student>.
  • <name>, <grade>, and <age> are children of <student> and siblings to each other.

Repeating Elements

In XML, the same element name can be used multiple times inside a parent. This is a common pattern when working with lists of items.

<fruits>
  <fruit>Apple</fruit>
  <fruit>Banana</fruit>
  <fruit>Mango</fruit>
</fruits>

All three <fruit> elements are valid siblings under the same parent <fruits>.

Practical Example: A Product Catalog Entry

<?xml version="1.0" encoding="UTF-8"?>
<product>
  <id>P-2045</id>
  <name>Wireless Mouse</name>
  <category>Computer Accessories</category>
  <price>24.99</price>
  <stock>
    <quantity>150</quantity>
    <warehouse>East Wing</warehouse>
  </stock>
  <available />
</product>

This example demonstrates text content, nested child elements, and an empty self-closing element all working together in one well-formed XML document.

Key Points

  • An element consists of a start tag, content, and an end tag.
  • Element content can be text, child elements, a mix of both, or nothing at all.
  • Empty elements can use self-closing tag syntax.
  • Element names must follow strict naming rules and are case-sensitive.
  • Elements form a tree structure with parent, child, and sibling relationships.
  • The same element name can be repeated multiple times within a parent.

Leave a Comment

Your email address will not be published. Required fields are marked *