What is XML? How to read and use XML files
Author: Domeny.ForSale editorial team Published
In brief: XML is a text format for storing and exchanging data, with tags that describe the meaning and structure of the information. Its uses include system integrations, configuration files and sitemaps. You can read an XML file in a text editor, but using it correctly requires an understanding of the rules adopted by the particular system.
How XML works and what its name means
XML stands for Extensible Markup Language. A tag is a label enclosed in angle brackets, such as <price>. It identifies what the value between the tags means.
“Extensible” means that XML does not impose a single set of tag names. The designer of a format can define elements such as <product>, <order> or <customer>. However, systems exchanging data must agree on the names and structure: using XML alone will not make a file understandable to every program.
XML primarily describes data, not its appearance. A file containing a product’s price and name does not automatically specify how they should appear on a shop’s website. Presentation is handled by the application or by additional processing rules.
How to read an XML file: a simple example
A single product might be recorded like this:
<?xml version="1.0" encoding="UTF-8"?>
<product id="123">
<name>Ceramic mug</name>
<price currency="PLN">39.90</price>
<available>true</available>
</product>
The first line is an optional XML declaration. It specifies the language version and character encoding—the way letters and other characters are stored in the file. If the declaration specifies UTF-8, the file’s actual encoding should match it.
<product> is the root element, which contains the other elements. <name>, <price> and <available> contain values. Meanwhile, id="123" and currency="PLN" are attributes: additional information placed in the opening tag.
Element names suggest what the data means, but do not define all the rules. XML itself does not determine whether a price must have two decimal places or whether true means that an item is in stock. These rules come from the format’s documentation or data schema.
What rules must valid XML follow?
A syntactically correct document is called well-formed. It must have one root element, and its elements must be properly closed and nested. <product><name>Mug</product></name> is incorrect because the closing tags appear in the wrong order.
When reading or correcting a file, the most important rules are:
- Names are case-sensitive:
<Price>and<price>are different names. - Attribute values must be enclosed in single or double quotation marks.
- An empty element can be written as
<notes/>instead of<notes></notes>. - Some characters in the content require special notation:
&is written as&, and<as<.
Correct syntax does not necessarily mean that the file meets the receiving system’s requirements. You can check its structure using an XSD schema: a document that describes the permitted elements, their order and the types of values they can contain. A file can be well-formed XML yet still be rejected by an application, for example because an order number is missing.
More complex documents also use namespaces. These distinguish elements that have the same name but come from different formats. For this reason, you should not remove prefixes such as abc: or xmlns declarations just because they look unnecessary.
Where XML is used and how it differs from HTML and JSON
You will encounter XML in data exchanges between shops and wholesalers, business system integrations and some web services. It is also used in RSS, a format for sharing content updates, and SVG, a vector graphics format. XML files are also found inside DOCX and XLSX documents.
An XML sitemap contains page URLs and can provide search engines with additional information about them. However, it does not guarantee indexing or any particular position in search results.
HTML describes the structure of web pages using predefined elements, such as headings and paragraphs. XML lets you create your own element names for storing data. JSON is another text-based data exchange format, based mainly on name–value pairs and lists.
For simple web application integrations, JSON is often more convenient and concise. XML may be an appropriate choice when the receiving system or an existing standard requires it. For example, if a wholesaler provides a catalogue in XML, the shop needs an importer that supports that catalogue’s specific structure—not merely arbitrary files with the .xml extension.
How to open XML and use it safely
A text editor is enough to view a file. A code editor will also highlight tags and help you find errors. A web browser may display the XML structure or a syntax error message, but it cannot replace checking whether the file meets the application’s requirements.
Before importing, make a copy of the file, check the required format in the receiving system’s documentation and test a small sample of the data. Do not upload files containing customer data, passwords or business information to random online validators.
If you are building an application that reads XML from an unknown source, configure its parser—the program that analyses the document—securely. Restrict external entity handling and the fetching of external resources so that a file cannot trigger unwanted access to files or the network.
A practical first step: open the XML file in an editor, find the root element and compare its structure with the documentation for the system into which you want to import the data.
See also: What is 2FA?
