Data is everywhere. It’s in our software tools to help them run. It’s in our websites to represent our content. And it’s in our browser cache and cookies to improve our user experience.
Of course, data isn’t just out there floating in the ether. Everything you see online needs to be stored and transferred from one piece of software to yours. On top of this, the amount of content being produced and shared online is increasing exponentially — and we need a way to accommodate this growth.
Allow me to introduce XML, one of the most popular and efficient ways of storing and moving data online. You might not know it, but many of your favorite software programs and web apps rely on XML to function. So, it’s worth spending some time getting to know this important technology.
In this guide, I’ll explain what XML means, what it does, how to open an XML file, and even how to construct a simple XML file yourself. It’s going to get a bit technical, but I promise this knowledge will make a worthy addition to your website development tool belt. Let’s dive in.
What Is an XML File?
XML stands for “extensible markup language.” XML is one of the most common tools used to store and transport content over the internet. An XML file contains XML code, and ends with the file extension ".xml".
Many software applications are built to read XML files, and, with just a bit of knowledge of how they work, so can you. Let’s break down the meaning of XML word-by-word.
XML is a programming language, one that can be read and used by computer software. Specifically, the purpose of XML is to store data in a way that can be easily read by and shared between software applications. XML doesn’t do anything with the data other than store it, like a database.
Let’s look at a basic example of an XML file below. You can also click here to view the file directly in your browser.
As you can see, this file consists of two things, plain text (in black) and tags (in violet).
Plain text is the actual data being stored. In this example, the XML is storing food items on a breakfast menu, as well as the things associated with each food: price, description, and calorie count.
While plain text represents the data, tags indicate what the data is. Each tag represents a type of data, like “food” or “price”, and tells the computer what to do with the plain text data inside of it. Tags aren’t supposed to be seen by the software user, only the software itself.
Each instance of an XML tag is called an element. In an XML file, elements are arranged in a hierarchy, which means that elements can contain other elements. The topmost element is called the “root” element, and contains all other elements, which are called “child” elements.
In the example above, "breakfast_menu" is the root element. It contains five “food” elements, and each “food” element contains the elements “name”, “price”, etc. The beginning and end of each element are represented by a starting tag (e.g., “<food>”) and a closing tag (e.g., “</food>”) respectively.
Also, you’ll often see XML code formatted such that each level of element is indented, as is true in our example. This makes the file easier for humans to read, and does not affect how computers process the code.
Tags are what make XML a markup language. The term “markup” comes from the practice of book editors “marking up” author manuscripts using a pencil, instructing authors what to change in the text.
You’ve probably heard of another markup language, the Hypertext Markup Language, or HTML. HTML is used to display content on web pages. Like XML, HTML contains both text (and usually other content too) and tags that instruct the software on how to use it. However, while XML tags specify the type of data, HTML tags specify how data is displayed. For example, the HTML tag “<p>” denotes a paragraph of text, and the “<b>” tag bolds text.
XML and HTML are not interchangeable languages, but they can be used together. HTML files often source their data from XML files.
Besides their purpose, there’s one other key difference between XML and HTML tags.
When programming in HTML, a developer must use tags from the HTML tag library, a standardized set of tags. While you can do a lot with these tags, there is a limited number available. That means there are only so many ways you can structure content on a web page.
XML does not have this limitation, as there is no preset library of XML tags. Instead, developers can create an unlimited number of custom tags to fit their data needs. This extensive customization is the “X” in XML.
To create custom tags, a developer writes a Document Type Definition (DTD), which is XML’s version of a tag library. An XML file’s DTD is indicated at the top of the file, and tells the software what each tag means and what to do with it.
For instance, an XML file containing info for a reservation system might have a custom “<res_start>” tag to define a time when a reservation begins. By reading the DTD, a program processing this file will know what the code “<res_start>7:00 PM PST</res_start>” means, and can use the information within the tag accordingly. This could mean sending this data in a confirmation email or storing it in another database.
To summarize: An XML file is a file used to store data in the form of hierarchical elements. Data stored in XML files can be read by computer programs with the help of custom tags, which indicate the type of element.
What is an XML file used for?
Since XML files are plain text documents, they are easy to create, store, transport, and interpret by computers and humans alike. This is why XML is one of the most commonly used languages on the internet. Many web-based software applications store information and send information to other apps in XML format.
Here are the most common uses of XML today:
Transporting Digital Information
The text-based format of XML files makes them highly portable, and therefore widely used for transferring information between web servers. Certain APIs, namely SOAP APIs and REST APIs, send information to other applications packaged in XML files.
XML files allow computer apps to easily structure and fetch the data that they need. After retrieving data from the file, programs can decide what to do with the data. This could mean storing in another database, using it in the program backend, or displaying it on the screen.
Additionally, some popular file formats are built with XML. Consider the Microsoft Office file extensions .docx (for Word documents), .xlsx (for Excel spreadsheets), and .pptx (for PowerPoint presentations). The “x” at the end of these file extensions stands for, you guessed it, XML.
Websites and Web Apps
Websites can pull content for their web pages from XML files. This is a common example of how the markup languages XML and HTML work together.
XML code modules might even appear within an HTML file in order to help display content on the page. This makes XML especially applicable to interactive websites and pages whose content changes dynamically. Depending on the user or screen size, an HTML file can choose to display only certain elements in the XML code, providing visitors with a personalized browsing experience.
How to Open an XML File
Since XML files are text files, you can open them in a few different ways. If you’re occasionally viewing XML files, you can open them directly in your favorite browser. If you’re frequently viewing, editing, and reformatting XML files, use an online XML editor or a text editor on your computer.
In this section, I’ll cover how to open XML files with each of these tools.
How to Open XML Files With a Web Browser
All modern web browsers allow you to read XML files right in the browser window. Like with the menu example from earlier, you can select an XML file from your device and choose to open it with your web browser.
While the appearance of the text will differ by browser, you should be able to easily parse the contents of the file, and you might also be able to hide and reveal specific elements.
If there’s an error in the file, your browser will tell you with an error window. Google Chrome will display an error message like the following:
Note that your browser won’t let you edit the file this way. To change the file, you’ll need to use a specialized tool.
How to Open XML Files With an Online XML Editor
You can use a free online text file editor to view your XML files, change their contents, or convert them to other file formats. We recommend Code Beautify’s XML Viewer for this purpose.
In the tool, click Browse to upload a file from your computer. Once uploaded, you can edit the file on the left and view the hierarchy of the XML contents on the right.
Once finished editing, click Save & Share to create a fresh XML file.
Code Beautify also offers many free conversion tools to convert your XML files to other popular data storage formats like JSON and CSV.
How to Open XML Files With a Text Editor
As with any text file, you can open XML files in any text editor. However, common editors like Notepad and Word probably won’t display your XML files with colors or indentation. This makes the files less readable, as seen in the example below.
You’ll want to opt for a specialized text editor that will detect the .xml format and display your files accordingly. For PCs, Notepad++ is a popular option. For Macs, try Xmplify or Eclipse.
If any of your systems implement XML files, they will almost certainly write all of these files for you. If you want to practice writing your own basic XML files, you can do so in a text editor. To create an XML file:
1. Open your text editor of choice. I'll be using Sublime Text.
2. On the first line, write a declaration to tell the application running the file that the language is XML:
3. Set your root element. Every XML file has one root element, which contains all other child elements.
Here, “<root_element>” is the starting tag for the root element, and “</root_element>” is the closing element. All other elements will go between these tags. You can substitute “root_element” in both tags with a name relevant to the information you're storing.
4. Add your child elements within the root element. Like the root element, each child element needs a starting tag and a closing tag. After adding child tags, your file will look something like this:
Instances of “root_element”, “child_element”, and “Content” can be swapped with names that make more sense for your file.
5. Save your file in the XML format using the .xml file extension.
6. Test your file by opening it in the browser window.
It might not be as engaging as parallax scrolling or as groundbreaking as machine learning, but XML is one of the most crucial technologies on the web today. You can leave the coding up to developers, but having a solid understanding of XML will give you a better sense of how websites, including your own, deliver content.
Originally published Jul 27, 2020 7:00:00 AM, updated August 04 2020