Cheatsheet: XML

Basic syntax

XML Declaration: The first line of an XML document should always be an XML declaration that specifies the version of XML and the encoding used

<?xml version="1.0" encoding="UTF-8"?>

Elements: XML elements are the building blocks of an XML document and can contain text, attributes, and other elements. Elements start with a start tag and end with an end tag.

<element>Text content</element>

Attributes: Attributes are used to provide additional information about an element and are placed within the start tag.

<element attribute="value">Text content</element>

Namespaces: Namespaces are used to distinguish between elements with the same name but from different sources.

<prefix:element xmlns:prefix="namespace URI">Text content</prefix:element>

Comments: XML comments are used to add notes to the document and are ignored by XML parsers

<!-- This is a comment -->

CDATA: CDATA sections are used to include character data in an XML document that would otherwise be treated as markup.

<![CDATA[This is character data]]>

Entities: XML entities are used to represent special characters, such as < for <, > for >, & for &, etc.

&lt; for <
&gt; for >
&amp; for &