According to Statista, worldwide data creation reached a new high of 79 zettabytes in 2021. It's expected to continue to increase rapidly, reaching 181 zettabytes by 2025 — or ten times the amount of data produced in 2016. Since the amount of data being produced and shared online is increasing exponentially, we need a way to accommodate this growth.
Cue XML, one of the most popular and efficient ways of storing and moving data online. Understanding this technology is a crucial addition to your website development tool belt.
That's why we'll cover the following:
- what an XML file is
- what an XML file is used for
- how to open an XML file
- how to create a simple XML file
An XML file contains XML code and ends with the file extension ".xml". It contains tags that define not only how the document should be structured but also how it should be stored and transported over the internet.
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 plain text and tags. The plain text is shown in black and the tags are shown in green.
Plain text is the actual data being stored. In this example, the XML is storing student names as well as test scores associated with each student.
While plain text represents the data, tags indicate what the data is. Each tag represents a type of data, like "first name," "last name," or "score," and tells the computer what to do with the plain text data inside of it. Tags aren’t supposed to be seen by users, 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, "studentsList" is the root element. It contains two "student" elements. Each "student" element contains the elements "firstName," "lastName," "scores," etc. The beginning and end of each element are represented by a starting tag (e.g., "<firstName>") and a closing tag (e.g., "</firstName>") 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.
Let's take a closer look at the purpose and history of this language below.
XML, short for "eXtensible Markup Language," was published by the World Wide Web Consortium (W3C) in 1998 to meet the challenges of large-scale electronic publishing. Since then, it has become one of the most widely used formats for sharing structured information among people, computers, and networks.
Since XML can be read and interpreted by people as well as computer software, it is known as human- and machine-readable.
The primary purpose of XML, however, is to store data in a way that can be easily read by and shared between software applications. Since its format is standardized, XML can be shared across systems or platforms, both locally and over the internet, and the recipient will still be able to parse the data.
It's important to understand that XML doesn’t do anything with the data other than store it, like a database. Another piece of software must be created or used to send, receive, store, or display the data.
At this point, you might be thinking XML sounds a lot like another markup language, the Hypertext Markup Language (HTML). Let's take a closer look at the differences between these languages below.
XML vs HTML
Both XML and HTML contain text 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. In short, XML is used to represent and share structured information, whereas HTML is used to display content on web pages.
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, or 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.
Let's take a look at some use cases for this extensible language below.
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.
Since XML defines the type of information contained in a document, it's easier and more effective to search the web with than HTML, for example.
Let's say you want to search for songs by Taylor Swift. Using HTML, you'd likely get back search results including interviews and articles that mention her songs. Using XML, search results would be restricted to songs only.
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 XML.
Websites and Web Apps
Websites and web apps can pull content for their 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 programs.
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. Here's how a file looks in Google Chrome:
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.
Alternatively, you can use a simple text editor and apply indentation to your files with a free online XML formatter.
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. Let's walk through how to create an XML file below.
How to Create an XML File
- Open your text editor of choice.
- On the first line, write an XML declaration.
- Set your root element below the declaration.
- Add your child elements within the root element.
- Review your file for errors.
- Save your file with the .xml file extension.
- Test your file by opening it in the browser window.
1. Open your text editor of choice.
I'll use Sublime Text for this demo since it's free and works on macOs, Linux, and Microsoft operating systems.
2. On the first line, write an XML declaration.
This declaration tells the application running the file that the language is XML.
3. Set your root element below the declaration.
Every XML file has one root element, which contains all other child elements. The root element is written below the declaration.
In this example file, "<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.
Next, add your child elements between the starting and closing tag of the root element. You can nest a child element within another child 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. Review your file for errors.
Time to review. Are there any missing closing tags? Any rogue ampersands? Does the document type declaration appear after the first element in the document? These are just a few possible errors.
Notice that line 5 is highlighted below. That's because the closing tag of the "child_element_2" is missing a bracket.
6. Save your file with the .xml file extension.
As said above, an XML file ends with the file extension ".xml". So make sure to save your file with that extension.
7. Test your file by opening it in the browser window.
Finally, test that your file is working by dragging and dropping it into a new browser tab or window.
Frequently Asked Questions about XML Files
Still have questions about XML files? No problem. Here are a few frequently asked questions about this type of file, along with the answers.
Can I open an XML file in Excel?
Yes. Open Excel and click File > Open. Locate the XML file on your computer and click Open. An XML file will look something like this in Excel:
Can I open an XML file in Word?
Yes. Open Word and click File > Open. Locate the XML file on your computer and click Open. An XML file will look something like this in Word:
How do I convert an XML file into a PDF?
To convert an XML file into a PDF, you can use a free online tool like Convert XML to PDF online. Simply click the Choose File button, select the XML file from your computer, and click Open. Then click the Convert Now button.
The PDF file will be ready to view or download within a few seconds.
How do I recover an XML file?
Whether you've accidentally deleted an XML file, your disk drive has been corrupted, or you simply can't find the file you're looking for, you can recover an XML file easily with a file recovery software tool like iBeesoft Data Recovery.
Just download and open the tool on your computer, select the file type "Other Files," then select where you want to search on your computer, and click Scan.
You'll see a list of results. You can sort by .xml, select the file, and click Recover.
For a more in-depth look into this process, check out this step-by-step guide for recovering a lost XML file or repairing a corrupted file with iBeesoft.
How do I comment in XML?
To comment in an XML file, enclose the text within <!-- --> tags. Here's an example of a comment:
<!-- declarations for <head> & <body> -->
Note that this is the same syntax for commenting in HTML.
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.
Editor's note: This post was originally published in July 2020 and has been updated for comprehensiveness.