Step-by-Step Guide | Creating Your First XML Document

If you are new to XML, creating your first XML document can feel confusing. But don’t worry — XML is simple once you understand the rules. In this guide, we’ll walk through the basics of XML, its structure, and how you can build your first working XML file.

What is XML?

XML (Extensible Markup Language) is a markup language designed to store and transport data. Unlike HTML, which focuses on displaying data, XML is all about describing and structuring data in a way that both humans and machines can understand.

Basic Structure of an XML Document

Every XML document follows a set of rules:

  • It begins with an XML declaration.
  • It has a root element that contains all other elements.
  • Each element must have a start tag and an end tag.
  • Attributes can be added to elements for extra information.

Example of a very simple XML structure:

<?xml version="1.0" encoding="UTF-8"?>
<message>
  <title>Hello World</title>
  <body>This is my first XML document.</body>
</message>

Rules You Must Follow

  1. Every XML document must have one root element.
  2. Tags are case-sensitive (<Name> is not the same as <name>).
  3. All elements must be properly nested.
  4. Attributes must always be inside quotes.

Breaking these rules will make your XML invalid, and programs won’t be able to read it correctly.

Creating Your First XML File

  1. Open any text editor (Notepad, VS Code, Sublime).
  2. Type your XML declaration at the top: <?xml version="1.0" encoding="UTF-8"?>
  3. Create a root element. For example: <bookstore> </bookstore>
  4. Add child elements inside the root. Example: <bookstore> <book> <title>Learning XML</title> <author>John Doe</author> <price>29.99</price> </book> </bookstore>
  5. Save the file with the extension .xml, for example books.xml.

How to Check If Your XML is Valid

After writing your XML file, you need to validate it. A validator will check for errors like missing tags or incorrect nesting. You can use online tools such as the XML Validator available on XML.CAT to make sure your file is correct.

Common Mistakes Beginners Make

  • Forgetting to close tags.
  • Using unquoted attributes.
  • Mixing uppercase and lowercase tags.
  • Having more than one root element.

Example of invalid XML:

<note>
  <to>John</to>
  <from>Mary</from>
  <message>Hello</message>
<note>

Here, the last closing tag should be </note> but it is incorrect, which makes the XML invalid.

Why Learning XML Still Matters in 2025

Even though JSON has become popular, XML is still used in enterprise systems, APIs, web services, financial institutions, and healthcare applications. Understanding XML helps developers work with a wide range of systems and data formats.

Conclusion

Creating your first XML document is easy if you follow the rules. Start with a simple root element, add child elements, and always validate your code. With practice, you’ll be able to build more complex XML files for APIs, configurations, and integrations.