TL;DR – The XML file format stands for the Extensible Markup Language that is mostly used for information exchange and describing data. XML formatters are tools that help you decide upon the indentation levels, convert to other formats and to beautify XML files.
Contents
What is XML format?
It is a markup language that can be modified to match users’ needs by adding and defining building blocks or elements.
The format is similar to HTML, but there are no pre-set codes for it. You can create your own markup symbols to describe the structure of a file. For instance, you do not have to follow specific rules but can make up the symbol system relevant to your data. Here is an example of a simple, beautified XML code:
<?xml version="1.0"?>
<Hiring>
<Applicants>
<FirstName>Susan</FirstName>
<LastName>Morrison</LastName>
<ContactNo>5555555555</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>London</City>
</Address>
</Applicants>
</Hiring>
The XML file format also takes a role in defining, storing, and transferring data in a way that both computers and humans can understand.
Note: since the XML file is a text document file, it can be opened and edited with any text editor on your computer. For example, Notepad++ is a solid option since it lets you structure your files properly.
XML formatters
XML formatters are either online tools or applications available for download. You can upload your XML files and beautify them, meaning that the tool will automatically add indentations based on the logic and structure of your document.
The example of the XML file in the previous section was beautified. Here is how it would look without the XML formatters:
<?xml version="1.0"?>
<Hiring>
<Applicants>
<FirstName>Susan</FirstName>
<LastName>Morrison</LastName>
<ContactNo>5555555555</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>London</City>
</Address>
</Applicants>
</Hiring>
As you can see, the indented version is much more reader-friendly. It is also useful that XML formatters allow you to upload files. After that, you can download validated and beautified versions.
Here are some XML formatters online than you can use:
If you want to download an XML formatter, we recommend the XMLBlueprint. It is free and does not require registration.
How to create XML files?
Let's take a look at this XML file example.
<?xml version="1.0" encoding="UTF-8"?>
<availableshops>
<shops category="clothes">
<name>"Urban Outfitters"</name>
<city>Philadelphia</city>
</shops>
<shops category="food">
<name>7-Eleven</name>
<city>Denver</city>
</shops>
<shops category="make-up">
<name>Sephora</name>
<city>London</city>
</shops>
</availableshops>
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
XML declaration
The XML declaration is the first line of every XML file. It specifies the XML version and the character encoding used in the document.
How to declare XML: assuming the XML version you're using is 1.0, and character encoding is UTF-8, the declaration would look like this:
<?xml version="1.0" encoding="UTF-8"?>
Note: remember that it always starts and ends it with a question mark.
XML elements
The first element following the declaration is called a root element. It acts as the main tag to the content you're writing. It's a requirement for all XML files.
In the example, <availableshops>
is the root element. Sub-elements and attributes (optional) follow the root element in a document.
Note: one of the main rules of the XML format is that the document must contain one root element, containing the rest of the elements.
Here's a little breakdown of each part:
Sub-elements follow the root element. It is the content of XML files. In the example below, you'll see several examples of possible sub-elements:
<name>Product One</name>
<cost>$49.99</cost>
<shipping>$1.50</shipping>
How to write one: start with an opening tag <for example>
. Then write your intended text without quotation marks, and close the tag </for example>
.
XML attributes define and give information about the elements. They usually come within a name-value pair and can also be used to create new categories. In the example, you'll see product listing
as the name, and ABC products
as the value.
How to write one: the structure is <name = "value">
. Remember that the value should always be written within quotation marks.
To create a new category, simply write code with an additional closing tag for the category at the end:
<product>
<product listing = "ABC products">
sub-elements
</product listing>
</product>
Rules to remember
- Always nest the elements properly. Incorrect and inconsistent nesting may cause errors in your XML files. The correct example of nesting is as follows:
<b><i>The bold element should go before the italic one</i></b>
. - Closing tags are necessary for all elements. It means that elements in XML files need to be followed by closing tags
</>
. Otherwise, they won't work. - Codes are case-sensitive. Codes should be written consistently throughout your XML file with no random upper-case or lower-case letters. For example:
<name>
should be followed with</name>
and not</Name>
.
XML formatter: useful tips
- The use of attributes in XML is optional, as they are harder to manage and to change in the future.
- Create markup symbols that are understandable to other people. This tip is especially important if you work with a group of people.
- The XML format can be used to manage content in multiple languages.