.xml

Term side of the Card.
Topic: Document & Data Formats
Unit title: .xml
Categories: Format
Explanation side of the Card

Explanation: 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.

Example

Wrongxml
<?xml version="1.0" encoding="UTF-8"?>
<show>
  <title>Tom & Jerry</title>
</show>
Rightxml
<?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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy