🌐 English

Home › Guides

XML Basics: Tags, Attributes and Structure

XML has been used for a long time to exchange documents and data, and you still meet it in settings files, product feeds and public data sets. Once you can see its structure, it is easy to read.

Tags and elements

An XML element starts with an opening tag like <name> and ends with a matching closing tag </name>. Between them sits text or other elements. For example: <item><title>Pen</title><price>1200</price></item>.

Opening and closing tags must match, and the whole document needs exactly one root element. An element with no content can be closed in one go, such as <empty/>.

Attributes and text

An element can carry attributes inside its opening tag. In <item id="A1" stock="3"> the id and stock are attributes, and their values are always in quotation marks. Whether information is stored as an attribute or a child element is the author's choice, so files differ.

Characters between tags are text. Line breaks and indentation spaces can also count as text, so keep that in mind when reading.

Reading the hierarchy

XML is shaped like a tree. Under the root there are child elements, and under them more children. When an element with the same name repeats, it acts like a list: three <item> elements inside an <order> form a list of three items.

Follow the indentation to count levels and look for repeating elements, which are usually the rows. When you flatten XML into a table, each repeated element normally becomes one line.

What changes in JSON

XML and JSON have similar structure, but they do not map one to one. Elements that have both attributes and text, and the difference between an element that appears once and one that repeats, affect the converted result.

The XML to JSON tool on this site converts inside your browser. After converting, look at how attributes are named and whether repeated elements are grouped into a list.

Checks before converting

XML is strict, and a single missing closing tag makes a file unreadable. If a conversion fails, suspect the structure first and open the file to look.

Files from other systems sometimes declare one encoding on the first line but are saved in another. If non-English letters look garbled, save the file again as UTF-8.