XML Elements and Attributes

In this tutorial, the basic concept of XML elements and attributes are described. An XML document contains XML Elements. An XML element is everything from the element’s start tag to the element’s end tag (including both).

An element can contain other elements, simple text or a mixture of both. XML elements can have attributes in the start tag, just like HTML. Attributes provide additional information about elements.

 The following example is an XML document with some elements and attributes:

<?xml version="1.0" encoding="UTF-8"?>
<library>
<book category="ENGINEERING">
   <title>Introduction to Algorithms</title>
   <author>Thomas H. Cormen et al</author>
   <edition>3rd</edition>
   <price>₹1,125</price>
  </book>
<book category="CHILDREN">
  <title>The Blue Umbrella</title>
  <author>Ruskin Bond</author>
  <edition>2nd</edition>
  <price>₹111</price>
  </book>
<book category="BIOGRAPHY">
<title>Notes of a Dream</title>
<author>Trilok Krishna</author>
<edition>1st</edition>
<price>₹304</price>
  </book>
</library>

In the example above, <library> and <book> have element contents, because they contain other elements. <author> has text content because it contains text.

In the example above only <book> has an attribute (category=”ENGINEERING”).

Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person’s gender, the person tag can be written like this:

<person gender=”male”>

or like this:

<person gender=’male’>

 

 

XML Elements vs. XML Attributes

There are no rules about when to use attributes and when to use elements. Attributes are handy in HTML. But in XML they are difficult to read and maintain. It is better to use elements for data and to use attributes for information that is not relevant to the data.

In XML it is recommended to avoid attributes. Use elements instead.

Take a look at these examples:

First Example

<?xml version=”1.0″ encoding=”UTF-8″?>

<person gender=”male”>
   <firstname>Ruskin</firstname>
   <lastname>Bond</lastname>
</person>

Second Example

<?xml version=”1.0″ encoding=”UTF-8″?>

<person>
  <gender>male</gender>
  <firstname>Ruskin</firstname>
  <lastname>Bond</lastname>
</person>

In the first example gender is an attribute. In the second, gender is an element. Both examples provide the same information.

 

The following three XML documents contain exactly the same information:

A date attribute is used in the first example:

<?xml version=”1.0″ encoding=”UTF-8″?>

<note date=”10/01/2010″>
  <to>Ram</to>
  <from>Shyam</from>
  <heading>Reminder</heading>
  <body>On Sunday we will watch Avengers!</body>
</note>

A date element is used in the second example:

<?xml version=”1.0″ encoding=”UTF-8″?>

<note>
  <date>10/01/2010</date>
  <to>Ram</to>
  <from>Shyam</from>
  <heading>Reminder</heading>
  <body>On Sunday We will watch Avengers!</body>
</note>

An expanded date element is used in the third:

<?xml version=”1.0″ encoding=”UTF-8″?>

<note>
  <date>
    <day>10</day>
    <month>01</month>
    <year>2010</year>
  </date>
  <to>Ram</to>
  <from>Shyam</from>
  <heading>Reminder</heading>
  <body>On Sunday We will watch Avengers!</body>
</note>

 

Some of the problems with using attributes are:

  • attributes cannot contain multiple values (elements can)
  • attributes cannot contain tree structures (elements can)
  • attributes are not easily expandable (for future changes)​