Home

XML Elements

 

Elements Fundamentals

 

Introduction

An element in an XML document is an object that starts with a start-tag, may contain a value, and may end with an end-tag. Based on this, the combination of a start-tag, the value, and the end-tag is called an element. An element can be more than that but for now, we will consider that an element is primarily characterized by a name and possibly a value.

The Name of an Element

The name of an element is the string that represents the tag. For example, in <Director>, the word Director is the name of the element. An element must have at least a start-tag. All of the tags we have seen so far were created as elements. When creating your elements, remember to follow the rules we defined for names.

The Text or Value of an Element

The value of an element is the item displayed on the right side of the start-tag. It is also called the text of the element. In the case of <Director>Jonathan Lynn</Director>, the "Jonathan Lynn" string is the value of the Director element.

While the value of one element can be a number, the value of another element can be a date. Yet another element can use a regular string as its value. Consider the following example:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
    <Employee>
	<FullName>Lydia Thomason</FullName>
	<Salary>25.64</Salary>
	<DepartmentID>1</DepartmentID>
    </Employee>
    <Employee>
	<FullName>June Grath</FullName>
	<Salary>16.38</Salary>
	<DepartmentID>4</DepartmentID>
    </Employee>
</Employees>

Notice that the Salary elements contain numbers that look like currency values and the DepartmentID elements use an integer as value.

Character Entities in an Element Value

Besides these obvious types of values, you may want to display special characters as values of elements. Consider the following example:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
    <Employee>
	<FullName>Sylvie <Bellie> Aronson</FullName>
	<Salary>25.64</Salary>
	<DepartmentID>1</DepartmentID>
    </Employee>
    <Employee>
	<FullName>Bertrand Yamaguchi</FullName>
	<Salary>16.38</Salary>
	<DepartmentID>4</DepartmentID>
    </Employee>
</Employees>

If you try using this XML document, for example, if you try displaying it in a browser, you would, receive an error:

An XML file in a browser

The reason is that when the parser reaches the <FullName>Sylvie <Bellie> Aronson</FullName> line, it thinks that <Bellie> is a tag but then <Bellie> is not closed. The parser concludes that the document is not well-formed, that there is an error. For this reason, to display a special symbol as part of a value, you can use its character code. There is a list of  parent of XML, provides a list of the characters for all special symbols. For example, the left angle bracket can be represented with &lt; while the right angle bracket can be represented with &gt;. Based on this, the above code can be corrected as follows:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
	<Employee>
		<FullName>Sylvie &lt;Bellie&gt; Aronson</FullName>
		<Salary>25.64</Salary>
		<DepartmentID>1</DepartmentID>
	</Employee>
	<Employee>
		<FullName>Bertrand Yamaguchi</FullName>
		<Salary>16.38</Salary>
		<DepartmentID>4</DepartmentID>
	</Employee>
</Employees>

This would produce:

XML also provides its own code to include special characters in your XML files.

Here is a list of other codes you can use for special characters:

Code Symbol Code Symbol Code Symbol Code Symbol Code Symbol
&apos; ' &#067; C &#106; j &#179; &#218;
&lt; < &#068; D &#107; k &#180; &#219;
&gt; > &#069; E &#108; l &#181; &#220;
&amp; & &#070; F &#109; m &#182; &#221;
&quot; " &#071; G &#110; n &#183; &#222;
&#033; ! &#072; H &#111; o &#184; &#223;
&#034; " &#073; I &#112; p &#185; &#224;
&#035; # &#074; J &#113; q &#186; &#225;
&#036; $ &#075; K &#114; r &#187; &#226;
&#037; % &#076; L &#115; s &#188; &#227;
&#038; & &#077; M &#116; t &#189; &#228;
&#039; ' &#078; N &#117; u &#190; &#229;
&#040; ( &#079; O &#118; v &#191; &#230;
&#041; ) &#080; P &#119; w &#192; &#231;
&#042; * &#081; Q &#120; x &#193; &#232;
&#043; + &#082; R &#121; y &#194; &#233;
&#044; , &#083; S &#122; z &#195; &#234;
&#045; - &#084; T &#123; { &#196; &#235;
&#046; . &#085; U &#125; } &#197; &#236;
&#047; / &#086; V &#126; ~ &#198; &#237;
&#048; 0 &#087; W &#160; empty &#199; &#238;
&#049; 1 &#088; X &#161; &#200; &#239;
&#050; 2 &#089; Y &#162; &#201; &#240;
&#051; 3 &#090; Z &#163; &#202; &#241;
&#052; 4 &#091; [ &#164; &#203; &#242;
&#053; 5 &#092; \ &#165; &#204; &#243;
&#054; 6 &#093; ] &#166; &#205; &#244;
&#055; 7 &#094; ^ &#167; &#206; &#245;
&#056; 8 &#095; _ &#168; &#207; &#246;
&#057; 9 &#096; ` &#169; &#208; &#247;
&#058; : &#097; a &#170; &#209; &#248;
&#059; ; &#098; b &#171; &#210; &#249;
&#060; < &#099; c &#172; &#211; &#250;
&#061; = &#100; d &#173; &#212; &#251;
&#062; > &#101; e &#174; &#213; &#252;
&#063; ? &#102; f &#175; &#214; &#253;
&#064; @ &#103; g &#176; &#215; &#254;
&#065; A &#104; h &#177; &#216; &#255;
&#066; B &#105; i &#178; &#217; &#256; A

There are still other codes to include special characters in an XML file.

Empty Elements

An element may not have a value but only a name. Consider the following example:

<?xml version="1.0" encoding="utf-8"?>
<Videos>
  <Video>
    <Title>The Distinguished Gentleman</Title>
    <Director>Jonathan Lynn</Director>
  </Video>
</Videos>

In this case, the Video element doesn't have a value. It is called an empty element but it is an element in its own right.

Parents and Child Nodes

An XML file appears as an upside-down tree: it has a root, can have branches and leaves. As we have seen in the previous lesson, all of these objects are created using the same technique: a tag with a name and an optional value. Based on their similarities, these objects can be referred to as nodes. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<Videos>
  <Video>
    <Title>The Distinguished Gentleman</Title>
    <Director>Jonathan Lynn</Director>
    <Length>112 Minutes</Length>
    <Format>DVD</Format>
    <Rating>R</Rating>
  </Video>
  <Video>
    <Title>Her Alibi</Title>
    <Director>Bruce Beresford</Director>
    <Length>94 Mins</Length>
    <Format>DVD</Format>
    <Rating>PG-13</Rating>
  </Video>
</Videos>

As seen in the previous lesson, one node can be nested inside of another. In that case, the nested node is called a child of the nesting node, which is a parent node. This also implies that a node can have as many children as necessary, making them child nodes of the parent node. In the above example, the Title and the Director nodes are children of the Video node. The Video node is the parent of both the Title and the Director node.

 

Previous Copyright 2007, Yevol Next