XML - Elements


Advertisements


XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these.

Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag.

Syntax

Following is the syntax to write an XML element −

<element-name attribute1 attribute2>
....content
</element-name>

where,

  • element-name is the name of the element. The name its case in the start and end tags must match.

  • attribute1, attribute2 are attributes of the element separated by white spaces. An attribute defines a property of the element. It associates a name with a value, which is a string of characters. An attribute is written as −

name = "value"

name is followed by an = sign and a string value inside double(" ") or single(' ') quotes.

Empty Element

An empty element (element with no content) has following syntax −

<name attribute1 attribute2.../>

Following is an example of an XML document using various XML element −

<?xml version = "1.0"?>
<contact-info>
   <address category = "residence">
      <name>Tanmay Patil</name>
      <company>TutorialsPoint</company>
      <phone>(011) 123-4567</phone>
   </address>
</contact-info>

XML Elements Rules

Following rules are required to be followed for XML elements −

  • An element name can contain any alphanumeric characters. The only punctuation mark allowed in names are the hyphen (-), under-score (_) and period (.).

  • Names are case sensitive. For example, Address, address, and ADDRESS are different names.

  • Start and end tags of an element must be identical.

  • An element, which is a container, can contain text or elements as seen in the above example.



Advertisements
E-Books Store