XML Elements
Overview
XML ( eXtensible Markup Language) is a markup language that defines rules for encoding documents in a human-readable and machine-readable format. XML elements are the building blocks of an XML document.
XML Elements
An XML document is made up of elements defined by tags. An element can contain text, other elements, or both.
Prolog and Declarations
XML documents can start with an XML declaration that specifies XML version and character encoding.
<?xml version=”1.0″ encoding=”UTF-8″?>.
Well-formedness: For an XML document to be considered “well-formed”, it must correctly follow the syntax rules of XML, such as proper nesting and closure of all tags.
XML Tags
Elements are defined by a start and an end tag. The start tag is denoted by <name> and the end tag by </name>. XML tags are case-sensitive. Therefore, <Title> and <title> are recognized as different elements.
For example, <message>Hello, World!</message> defines an element named “message” with the content “Hello, World!”.
All the XML elements are extensible and can be extended to carry more information. XML elements have relationships and follow naming rules.
- Names can contain letters, numbers & other characters.
- Names should not start with numbers or punctuation characters.
- Names cannot contain spaces.
XML Attributes
XML tags can have attributes that provide additional information about the element. Attributes are included in the start tag and are generally written as name-value pairs like attribute=”value”.
For example, <person name=”John” age=”28″/>.
Nesting of elements
Elements can be nested within other elements. This creates a hierarchical structure. Proper nesting is crucial; for example:
<company>
<employee name=”John”>
<age>28</age>
<occupation>Engineer</occupation>
</employee>
</company>
In this example, the employee element is nested within the company, and age and occupation are nested within an employee element.
Empty Elements: If an element has no content, it can be closed within the start tag, an empty element tag. For example, <empty /> is an empty element equivalent to <empty></empty>.
XML is widely used for storing and transporting data due to its simplicity and flexibility.