Creating an XML document
Like some other markup languages, XML relies heavily on three fundamental building blocks:
- Elements
- Values
- Attributes
An element is used to describe or contain a piece of information; elements form the basis of all XML documents. Elements consist of two tags: an opening tag and a closing tag. Opening tags appear as words contained within angle brackets (<>
), such as <pets>
or <friends>
. Closing tags also appear within angle brackets, but they have a forward-slash (/
) just before the tag name. Examples of closing tags are </pets>
and </friends>
. Elements always appear as an opening tag, followed by optional data, followed by a closing tag.
Creating an Empty Element. Following is an example of an empty element:
<pets></pets>
Creating an Element Contains Value. Following is an example of how the pets element can contain content (values):
<pets>Simba and Max </pets>
Creating Nested Elements. Following is an example of how the pets element can contain additional content, which in this case is a couple of pet elements (when elements are contained within other elements, they are known as nested elements):
<pets> <pet>Simba</pet> <pet>Max</pet> </pets>
Adding attributes. You can attach attributes to elements. Attributes are small pieces of information that appear within an element’s opening tag. An attribute consists of an attribute name and a corresponding attribute value. Following is an example of an attribute named name that is associated with the pet element:
<pets> <pet type="cat">Simba</pet> <pet type="dog">Max</pet> </pets>
You can use several different attributes with a single element. Following is an example of how several attributes are used to describe a pet in detail:
<pets> <pet type="cat" age="2">Simba</pet> <pet type="dog" age="3">Max</pet> </pets>
The key to XML’s accuracy lies in a few simple rules:
- Tag names are case sensitive.
- Every opening tag must have a corresponding closing tag.
- A nested tag pair cannot overlap another tag.
- Attribute values must appear within quotes.
- Every document must have a root element.
The root element. The root element is the single element in an XML document that contains all other elements in the document. Every XML document must contain a root element, which means that exactly one element must be at the top level of any given XML document. In the “pets
” example, the pets
element is the root element because it contains all the other elements in the document.
The XML Declaration. One final important topic to cover in this quick tour of XML is the XML declaration, which is not strictly required of all XML documents but is a good idea nonetheless. The XML declaration is a line of code at the beginning of an XML document that identifies the version of XML used by the document. Currently there are two versions of XML: 1.0
and 1.1
. As of XML 1.1, all XML documents are required to include an XML declaration. The XML declaration must be the first construct in the document. Following is the standard XML declaration for XML 1.0:
<?xml version="1.0"?>
XML Editor. To create and edit your own XML documents, you must have an application to serve as an XML editor. Because XML documents are raw text documents, a simple text editor can serve as an XML editor. For example, if you are a Windows user you can just use the standard Windows Notepad application to edit XML documents. Or on a Macintosh computer you can use TextEdit. Let’s open an text editor and save the following xml example as pets.xml
:
<?xml version="1.0"?> <pets> <pet type="cat" age="2">Simba</pet> <pet type="dog" age="3">Max</pet> <pet type="dog" age="5">Lucy</pet> <pet type="dog" age="5">Teddy</pet> </pets>
For detailed tutorials, visit following links: