The Extensible Mark-up Language or abbreviated to XML is a technique of using a document, such as a text document in a way to describe information and at the same time, make that piece of information accessible to anyone who would like to benefit from it. The description of the XML file is done in such a way that the information described by one person can be used by another person or institution without the knowledge of the original author of the document.
This is easy to do because the XML file created, is not a program nor an application but is a text-based document. In this article, we are going to look at an easy way to create an XML file using Notepad.
XML is an extremely flexible service and can be used in basic computer applications, database systems, web-based and scientific applications, to name a few.
Creating a file
To create an XML file, Open Notepad editor and type in the units of code using normal characters. XML documents have units called entities. These entities are spread out over the document, providing a well-defined structure to the document. XML requires a very specific structure, without which the contents of the document will not be considered valid.
As mentioned before, an XML file is a normal text-based document with an .xml extension. Therefore, while creating and saving it, it is important to specify that extension.
It is possible to save the file with .xml extension and in the Save as Type, select All Files and then enter the name of the file with the XML extension.
There are several applications out there, that will allow for you to generate an XML file from an existing file or allow you to create another one. There are editors that you can purchase commercially, to create an XML file.
Creating and saving the XML document is the first step of the process. Accompanied by this, you need a program or an application that can read, analyze, and interpret the content of the document. XML Files have XML parsers, that serves as an interface for people and organizations to work with the XML document. Its main purpose is to check for the proper formatting of the XML document and also to validate the authenticity of the documents.
Modern browsers usually have built-in XML parsers. The parser makes the XML code readable.
Writing a simple XML File
This is going to be the first step in creating an XML file. You can create the simplest version of an XML file, using it for a slide presentation. Now, to start with this exercise, you can use your text editor to create data to establish familiarity with the XML File.
The next step is going to be creating a standard file, titling it as creatingxmlsample.xml. As mentioned before, whether you opened an XML file or someone else did, you can view its contents. The easiest way to open an XML file is to use Notepad or any other text editor. If you are using a different application, then it should provide you with the File>open option.
You can also view the XML document through the browser. If you see the file on Windows Explorer or in My documents, then you can click on it to view it.
When you are creating an XML file, then there are prescribed rules that you must follow. These rules have to be adhered to, to have a valid XML document.
To do this, you have to create a mark-up. A mark-up is an already-set instruction that defines XML. The formula of a mark-up is:
<tag>
The left- hand bracket and the right- hand bracket is required. Without this, what you write in between, will not be valid. Inside of these brackets or symbols, you can write a word or a group of letters and alphabets in English which can also contain non-comprehensible characters such as “?” or “!”. The combination of the brackets on the left and the right-hand side and the symbols/words that are contained in between them is called the mark-up.
Putting down the declaration
One thing to understand is that XML is released as a version. Because there could be many versions, the first line that can be processed in an XML file must specify the version of XML you are currently using. An XML File should start with, in the top section, an XML declaration. We will show below what it should look like.
<?xml version=”1.0″?>
Encoding the Declaration
The tags are created using characters from the alphabet and this is usually done in conforming to the ISO standard. Most of the characters that are recognized by the English language are referred to as ASCII. This is so because they use a combination of 7 bits for a symbol. This kind of encoded declaration is called UTF-8.
In order to specify the coding that you are using, let us see what it would look like:
<?xml version="1.0" encoding="utf-8"?>
Adding Comments
Comments are not recognized by XML parsers. You will not be able to see comments unless you activate the specific settings required to see them. Let us look at what adding a comment is going to look like.
<?xml version='1.0' encoding='utf-8'?>
<! —CREATING XML SAMPLE -->
Defining the root element
This is such a beautiful way to describe one of the most important components of your XML file. The word ‘root’ as is self-explanatory, is going to be that one element after your declaration, within which are contained all the other elements. Let us look at the text below to identify the root element. For this- the root element is: XML samples
<?XML version='1.0' encoding='utf-8'?>
<! —CREATING XML SAMPLE -->
<xmlsamples>
</xmlsamples>
Adding Attributes
Just as we defined the root element, each presentation or document is going to have a list of associated elements that do not require any structure and do not validate the document. So, for this purpose alone, they are called attributes. For this example- they are going to be attributed to the XML samples element.
<xmlsamples
title="XML SAMPLE PRESENTATION"
date="Date of publication"
author="SAHIL"
>
</xmlsamples>
When you are creating a name or a tag for an attribute, you should use hyphens, underscores, periods, colons, in addition to numbers and characters. Values for XML attributes should always be kept in quotation marks and irrespective of the number of attributes, they are not separated by commas.
Nested Elements
XML allows for hierarchy and that means, that one element can contain other elements. Here, we are also going to be adding an attribute such as “type” and this is only to make its identity distinct, to ensure that it will be looked at by professionals.
<xmlsamples
...
>
<! -- TITLE SLIDE -->
<slide type="all">
<title>CREATING AN XML FILE! </title>
</slide>
</xmlsamples>
Through this example, let us observe the difference between elements that are extremely important for the document such as “Title” and the attributes such as the “type” attribute. In simple terms, the title is something that people are going to see, so it is presented this way. The attribute will not be visible to the audience; hence it is an attribute.
Adding HTML type text
Since XML is pretty flexible when it comes to choosing the type of tags you can include in your document, it is better to use HTML tags. Let us look at the text below to see the HTML-type tags that you can use in your document.
…
<!-- TITLE SLIDE -->
<slide type="all">
<title>Creating an XML File!</title>
</slide>
<!-- OVERVIEW -->
<slide type="all">
<title>Overview</title>
<item>Howto <em>Create</em> XML File</item>
<item>Simple Steps <em></em> t</item>
</slide>
</xmlsamples>
There is one major difference between HTML and XML, and that is each XML element must be well-formed, which implies that every tag should have a starting tag and an ending tag. We can also add an empty element with no content. Let us look at how we can do that.
...
<!-- OVERVIEW -->
<slide type="all">
<title>Overview</title>
<item>Howto <em>Create</em> XML File</item>
<item>Simple Steps <em></em> t</item>
</slide>
</xmlsamples>
An important thing to note is that every element can be an empty element. All it takes is, “/> in place of “>”.
Now, using the elements and the process that we have followed above, we invite you to write down what the finished piece would look like.
Once you have created a file that you are comfortable with, you are ready to echo it using an XML parser. For now, work on stringing these beautiful threads together and create your very own XML file. Happy writing!