.xml
Example
<?xml version="1.0" encoding="UTF-8"?>
<show>
<title>Tom & Jerry</title>
</show><?xml version="1.0" encoding="UTF-8"?>
<show>
<title>Tom & Jerry</title>
</show>A raw & starts an entity in XML, so it must be written as & 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
&,<and>, 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.