3. XML Elements

What you will learn

  • What an XML element is
  • Naming rules and good naming habits
  • Elements with text, with children, and with both
  • Empty elements, repeating elements, and CDATA

What is an element?

An element is everything from a start tag to its matching end tag. It can contain text, other elements, or both.

XML code

<price>19.99</price>

Here the element is named price, and its content is the text 19.99. XML does not know this is a number. To a parser it is just text, until a schema (a later lesson) says otherwise.

Naming rules

  • Names may contain letters, digits, hyphens, underscores, and full stops.
  • They must start with a letter or an underscore, not a digit or punctuation.
  • They cannot contain spaces.
  • They cannot start with the letters xml in any capitalisation, because those are reserved.
  • They are case sensitive.
Name Valid?
first_name Yes
firstName Yes
item2 Yes
2item No: starts with a digit
first name No: contains a space
xmlData No: starts with xml

Good naming habits

  • Use clear, descriptive names: <birthDate> beats <bd>.
  • Pick one style, either lower_case or camelCase, and use it everywhere.
  • Avoid dashes and full stops in names. Some software reads them as minus signs or as property access.
  • Avoid non-English letters in names if you may share the file with other systems, even though they are allowed.

Text content

XML code

<title>Learning XML</title>

Child elements

Elements can contain other elements. The one that contains is the parent, and the ones inside are its children. Children of the same parent are siblings.

XML code: students.xml

<?xml version="1.0" encoding="UTF-8"?>
<students>
  <student>
    <name>Sam</name>
    <score>72</score>
  </student>
  <student>
    <name>Ali</name>
    <score>90</score>
  </student>
</students>

The structure

students  (the root)
 ├── student
 │    ├── name  = Sam
 │    └── score = 72
 └── student
      ├── name  = Ali
      └── score = 90

Repeating the same element (student) is how XML stores a list.

Mixed content

An element can hold text and child elements together. This is common in documents, and rare in plain data.

XML code

<paragraph>XML is <emphasis>strict</emphasis> but dependable.</paragraph>

Empty elements

An element with no content can be written two ways. They are identical to a parser.

XML code

<discount></discount>
<discount/>

An empty element often carries its information in attributes, as the next lesson shows.

Missing and empty values

XML has no built-in “null”. People use an empty element, or leave the element out, and agree what that means. Whichever you choose, document it, and be consistent.

CDATA: raw text

A CDATA section holds text that the parser must not interpret. It is handy for code samples, because you do not have to escape every < and &.

XML code

<snippet><![CDATA[
if (a < b && c > d) {
  print("Hello");
}
]]></snippet>

What the text means after parsing

if (a < b && c > d) {
  print("Hello");
}

The only thing a CDATA section cannot contain is its own ending, ]]>.

Elements or attributes?

You will often choose between two ways to store a piece of data. A common guide: use elements for the data itself, and attributes for information about the data (such as an ID or a unit). The next lesson goes into this.

Common mistakes

  • Starting a name with a digit, or including a space.
  • Using different styles (first_name and lastName) in one file.
  • Choosing short, cryptic names.
  • Forgetting that numbers are just text unless a schema says otherwise.
  • Putting a raw & or < in text instead of escaping it or using CDATA.

Exercise

Write an XML file that stores two products. Each product has a name and a price. Use a sensible root element, and repeat product for each one.

Show solution
<?xml version="1.0" encoding="UTF-8"?>
<products>
  <product>
    <name>Notebook</name>
    <price>4.50</price>
  </product>
  <product>
    <name>Pen</name>
    <price>1.20</price>
  </product>
</products>

Knowledge check

1. Can an element name contain a space?

No.

2. How does XML store a list of similar things?

By repeating the same element inside a parent.

3. What is a CDATA section for?

Holding text that the parser must not interpret, so you do not need to escape special characters.

Summary

  • An element is a start tag, content, and an end tag. Content can be text, children, or both.
  • Names start with a letter or underscore, have no spaces, and should be clear and consistent.
  • Repeat elements for lists, use empty elements for no content, and CDATA for raw text.
Scroll to Top