XML: What It Is, Document Structure and How It Works

Author: IT Sectr Published: 2026-03-08 Reading time: 8 min

XML (eXtensible Markup Language) is a text format for storing and transmitting structured data using custom tags. It is widely used in configuration files, web services and Android development. According to the W3C consortium, the XML specification has remained a stable standard since 2008. XML provides a strict data structure with the ability to validate through schemas.

Key Takeaways

  • XML — an extensible markup language with custom tags for structured data
  • Strict structure — mandatory closing tags, root element and validation via XSD
  • Namespaces — a mechanism to avoid name conflicts within a single document
  • XSLT — transformation of XML into other formats: HTML, PDF, CSV
  • Android development — XML is used for layouts, manifest and resources

What Is XML?

XML is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. Unlike HTML, XML does not have a fixed set of tags — developers create their own tags that correspond to the data structure.

XML was developed by the W3C consortium in 1996 as a simplified subset of SGML (Standard Generalized Markup Language). The main goal of XML is to separate content from data presentation. Unlike HTML, which describes appearance, XML describes the structure and meaning of data.

The key property of XML is extensibility. If new data types emerge in a project, the developer simply adds new tags without changing the existing structure. This makes XML convenient for long-term data storage and exchange between different systems.

Main Areas of XML Application

  • Android development — layout files, manifest, resources (strings, colors, themes)
  • Configuration — Maven POM, Ant build, web configs (web.xml)
  • Web services — SOAP, XML-RPC, RSS/Atom feeds
  • Document workflow — SVG, DocBook, Office Open XML
  • Databases — SQL Server (XML type), Oracle, PostgreSQL

XML Syntax and Structure

XML syntax is stricter than HTML. Every opening tag must have a corresponding closing tag, attributes are enclosed in quotes, and tag names are case-sensitive. The document must have exactly one root element that contains all others.

An XML document may begin with a declaration specifying the XML version and encoding. This is followed by the root element, which may contain nested elements, attributes, text content and comments. Empty elements are written as .

xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <book id="bk001">
        <title>XML Developer Guide</title>
        <author>John Smith</author>
        <price currency="USD">29.99</price>
        <published>2026-03-15</published>
    </book>
    <book id="bk002">
        <title>Advanced XML Schemas</title>
        <author>Jane Doe</author>
        <price currency="USD">49.99</price>
        </book>
</catalog>

Rules for Well-Formed XML

Well-formed XML must follow basic rules: closing tags are mandatory for every opening tag, nesting is strict (tag overlapping is forbidden), attribute values are always in quotes. Violating any rule makes the document invalid and unparsable.

  1. One root element — the document contains exactly one root
  2. Correct nesting — tags must not overlap
  3. Closing tags — mandatory for all elements except self-closing ones
  4. Case sensitivity — <Book> and <book> are different elements
  5. Quotes in attributes — attribute values in double or single quotes

XML Namespaces and Schemas

XML Namespaces solve the problem of name conflicts when elements from different vocabularies are used in one document. A namespace is defined by the xmlns attribute with a URI that uniquely identifies the vocabulary.

XML Schema (XSD) is a language for describing the structure of an XML document. XSD defines which elements and attributes are allowed, their data types, mandatory status and constraints. Schemas allow automatic document validation before processing, which is critical for integration solutions.

Technology Purpose Example
DTD Basic structure validation Older, fewer capabilities
XSD Strict typing and validation Industry standard
RELAX NG Compact alternative to XSD Simpler and more readable than XSD
XSLT Transformation of XML into other formats XML to HTML, PDF, CSV
XPath Navigation and node selection Queries to XML tree

XML in Mobile Development

XML plays a key role in Android development. The Android operating system uses XML to describe the user interface (layout files), application configuration (AndroidManifest.xml), resources (strings, colors, themes) and vector graphics (VectorDrawable). On iOS, XML is used less frequently, mainly for Storyboard and configuration plist files.

In Android, each application screen is described by an XML file with a hierarchy of View components: ConstraintLayout, LinearLayout, TextView, Button. This approach separates application logic from presentation, simplifying maintenance and adaptation for different screens. Resource XML files allow localization of the application without changing code.

Android Layout Example

A typical layout file contains a root ConstraintLayout with nested elements, each having attributes for positioning, size and style. Android compiles XML into AXML binary format for performance optimization at startup.

xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/welcome"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click"
        app:layout_constraintTop_toBottomOf="@id/title"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

XML Parsing on Mobile Platforms

XML parsing on mobile platforms is done using two main approaches: DOM (Document Object Model) loads the entire document into memory as a tree, SAX (Simple API for XML) reads the document streamingly, calling event handlers. DOM is convenient for small documents, SAX is for large files with memory constraints.

Android has built-in parsers: XmlPullParser for stream parsing and DocumentBuilder for the DOM approach. iOS uses Foundation XMLParser with a delegate protocol. For serializing objects to XML, libraries like SimpleXML in PHP or Jackson XML in Java are often used.

XML Parsing on Android (Kotlin)

Android recommends using XmlPullParser for efficient XML parsing. The parser operates in event-driven mode: it calls next() to move to the next element and returns the event type (START_TAG, TEXT, END_TAG). This allows processing documents of any size with minimal memory consumption.

kotlin
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import java.io.IOException

fun parseCatalog(parser: XmlPullParser): List<Book> {
    val books = mutableListOf<Book>()
    var eventType = parser.getEventType()
    var currentBook: Book? = null

    while (eventType != XmlPullParser.END_DOCUMENT) {
        when (eventType) {
            XmlPullParser.START_TAG -> {
                when (parser.getName()) {
                    "book" -> currentBook = Book()
                    "title" -> parser.next()
                    "author" -> parser.next()
                }
            }
            XmlPullParser.TEXT -> {
                currentBook?.apply {
                    title = parser.getText() ?: ""
                }
            }
            XmlPullParser.END_TAG -> {
                if (parser.getName() == "book") {
                    currentBook?.let { books.add(it) }
                }
            }
        }
        eventType = parser.next()
    }
    return books
}

XML Examples in Applications

Use examples of XML demonstrate its role in real projects. In addition to Android development, XML is used for build configuration, data description and document workflow. Understanding XML syntax is necessary for working with Maven, SOAP services and many enterprise tools.

Maven Configuration (POM)

The pom.xml file describes a Maven project: dependencies, plugins, versions and build profiles. Each dependency is specified with groupId, artifactId and version tags. Maven automatically downloads the specified libraries from repositories.

xml
<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>retrofit</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
</project>

XML vs JSON: When to Choose Which

The choice between XML and JSON depends on the specific requirements of the project. JSON is suitable for REST APIs, mobile applications and configurations where compactness and speed are important. XML is irreplaceable in Enterprise systems, document workflow and Android development, where strict validation, namespaces and data transformation are needed.

XML wins in scenarios with long-term document storage. Thanks to XSD schemas, XML documents can be validated automatically, which is critical for medical, financial and legal systems. XSLT transformations allow generating HTML, PDF and other formats from a single XML source, simplifying document workflow.

JSON, in turn, has become the standard for web APIs and mobile applications. Ease of integration with JavaScript, smaller size and high parsing speed make JSON the preferred choice for REST APIs, WebSocket messages and configuration files. For most new projects, it is recommended to start with JSON and switch to XML only when specific requirements arise.

Frequently Asked Questions

How is XML different from HTML?

XML describes the structure and content of data, HTML describes the appearance and presentation. XML allows creating any tags, HTML has a fixed set. XML is stricter in syntax: all tags must be closed, HTML allows unclosed tags. XML is for data, HTML is for display.

What is XSD and why is it needed?

XSD (XML Schema Definition) is a language for describing the structure of an XML document. XSD defines which elements and attributes are allowed, their data types, mandatory status and constraints. The schema allows automatic validation of an XML document before processing, preventing errors and ensuring data integrity.

How to parse XML on Android?

On Android, use XmlPullParser for XML parsing — a built-in streaming parser. It works event-driven: processes START_TAG, TEXT and END_TAG in a loop, managing memory efficiently. For small documents, a DOM parser through DocumentBuilderFactory is suitable, which loads the entire document into memory as a node tree.

What is the difference between DTD and XSD?

DTD (Document Type Definition) is an old validation mechanism with limited capabilities: it does not support data types (everything is strings) and namespaces. XSD is a modern replacement with support for data types (numbers, dates, enumerations), namespaces and extensibility. XSD itself is an XML document, while DTD uses its own syntax.

Why are namespaces needed in XML?

Namespaces prevent name conflicts when an XML document combines elements from different sources. For example, in an Android layout, the View tag can come from both android.view and a library. The xmlns attribute with a URI uniquely identifies the source of each element and attribute.

Summary

  • XML — an extensible markup language with custom tags for structured data
  • Strict syntax — mandatory closing tags, root element, case sensitivity
  • Namespaces and XSD schemas provide validation and prevent name conflicts
  • Android uses XML for interfaces, resources and application configuration
  • Parsing is done via XmlPullParser (streaming) or DOM (in memory)
  • XSLT allows transforming XML into HTML, PDF and other formats
  • Less popularity in REST API gives way to JSON, but XML is irreplaceable in Enterprise and Android

We will develop a mobile application turnkey

IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.

Discuss the project

Read also