What is the CDATA in XML? [PHP Code]

CDATA (meaning character data) is used for different but related purposes in the SGML and XML markup languages. The term CDATA is used for textual data that should not be parsed by an XML parser. The characters "<" and "&" are not allowed within XML elements. The "<" character will throw an error because the parser will interpret it as the start of a new element. The "&" character will throw an error because the parser will interpret it as the start of a character entity. The parser completely ignores everything inside the CDATA section. The CDATA section starts with "<![CDATA[" and ends with "]]>". The PHP code was automatically generated for the XML Cdata example.
What is the CDATA in XML? [PHP Code] Format
<?xml version="1.0" encoding="UTF-8"?>
<Client>
   <order id="1">
   <login>login</login>
   <info><![CDATA[
     CDATA CODE with characters
   ]]></info>
  </order>
</Client>
Updated: Viewed: 23772 times
PHP code for XML Cdata example

PHP code for XML Cdata Example

This PHP code snippet was generated automatically for the XML Cdata example.
<< Back to the XML Cdata example

What is XML?

XML (Extensible Markup Language) is a markup language similar to HTML, but it has no predefined tags. Instead, you define your own custom tags explicitly designed for your needs. It's a powerful way to store data in a format that's easy to store, search, and share. The XML format is standardized, and if you share or transfer XML between systems or platforms (locally or over the Internet), the recipient can parse the data. An XML document may consist of comments, elements, declarations, and processing instructions. Each of these components must be structured using XML markup.

XML Example
<?xml version = "1.0" encoding = "UTF-8"?>
<Request>
  <Login>
     login
  </Login>
  <Password>
     password
  </Password>
</Request>

What is CDATA?

The XML character data (CDATA) is a type of node recognized by markup languages but not parsed by parsers. CDATA is used to solve the problem of including arbitrary data in an XML document. The data is only for a group of text that defines markup, such as characters. The CDATA section includes all markup characters exactly as they were passed to the application and excludes nesting. All XML object references and tags are parsed by the processor and treated as character data.

Following is the XML syntax for the CDATA. The CDATA section starts with "<!CDATA[" and ends with "]]>" tags. Characters between these tags are treated as text, not markup, and may include special characters. Embedded XML markup is not interpreted. Any content declared inside the "<![CDATA[ and ]]>" tag is ignored by the XML processor and is almost equivalent to a comment section in XML.

CDATA Syntax
<!CDATA[
  // [some data]
]]>

XML CDATA Examples

Below are examples of XML CDATA with a detailed description:

XML CDATA Example
<?xml version = "1.0" encoding = "UTF-8"?>
<client>
    <order id="1">
        <login>login</login>
        <info>
        <![CDATA[
some data
]]>
        </info>
    </order>
</client>

How to specify an HTML code in CDATA?

An example of including HTML code in CDATA:

HTML code in CDATA Example
<?xml version = "1.0" encoding = "UTF-8"?>
<xml>
    <code><![CDATA[
<code>
<HEAD><h2>XML Code Example</h2>description about file</p>
</body>
</code> ]]>
    </code>
</xml>

How to specify JavaScript code in CDATA?

The following is an example of including JavaScript code in CDATA:

JavaScript code in CDATA Example
<?xml version = "1.0" encoding = "UTF-8"?>
<xml>
  <code>
<![CDATA[
let message = (login == 'Employee') ? 'Hello' :
  (login == 'Director') ? 'Hello, boss' :
  (login == '') ? 'No login' :
  '';
]]>
  </code>
</xml>

What is the difference between CDATA and comments?

Below are the main differences between CDATA and comments:

  • The CDATA is part of the document, while a comment is not
  • The CDATA is used when large amounts of text need to be displayed in XML documents
  • Comments are ignored by XML parsers

See also

Generate code snippets for PHP and other programming languages

Convert your XML Cdata request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.