# .xml

> The `.xml` (eXtensible Markup Language) is a markup language for storing and transporting data, focusing on simplicity and flexible data representation.

## What is XML and how is it different from JSON?

The flexible text-based format used to store and transport data in a structured way. It uses custom tags to define elements, making it extensible and suitable for a wide range of applications, especially for web services.

- Topic: Document & Data Formats
- Type: format

## Example

Wrong:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<show>
  <title>Tom & Jerry</title>
</show>
```

Right:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<show>
  <title>Tom &amp; Jerry</title>
</show>
```

A raw `&` starts an entity in XML, so it must be written as `&amp;` for the document to parse.

## Common mistakes

- Writing a raw `&` in text, which makes the whole document invalid.
- Parsing untrusted XML with external entities enabled.
- Forgetting that XML is case-sensitive and every tag must close.

## Key points

- Special characters in text are escaped as `&amp;`, `&lt;` and `&gt;`, or wrapped in CDATA.
- The XML parser disables external entities and DTD loading to prevent XXE attacks.
- XML is built with a serializer or DOM API, not by joining strings with user input.

## Interview questions

### XML vs JSON: which should I use?

Use JSON for most web APIs because it is shorter and maps to objects. XML still fits documents, feeds, sitemaps, SVG and systems that use schemas.

### What is an XXE attack?

XML External Entity injection uses entity declarations to read server files or make network requests. Disable external entities in the parser to block it.

### Is HTML a kind of XML?

No. HTML has its own forgiving parser. XHTML and SVG follow XML rules, where every tag must close.

## Related cards

- [.json](https://wonderium.dev/engineering/json.md)
- [sitemap.xml](https://wonderium.dev/web/sitemap-xml.md)
- [SOAP](https://wonderium.dev/web/soap.md)
- [`<svg>`](https://wonderium.dev/html/svg.md)
- [.yml](https://wonderium.dev/engineering/yml.md)

---

- Level: [Engineering](https://wonderium.dev/engineering.md)
- HTML version: https://wonderium.dev/engineering/xml
- Last updated: 2026-11-25
