PCDATA - Parsed Character Data

It means parsed character data. For example:

<book>Yunus Emre Divani</book>

Inside the book element, you need to write & < > instead of & < >, otherwise it will process as if you're opening another element and you'll encounter an error.

To avoid errors, we need to use CDATA.

CDATA - Character Data

It means character data. We use CDATA where PCDATA is insufficient. For CDATA:

<book>
	<![CDATA[
	Yunus <emre> Divani
	]]>
</book>

It starts with . Where do we use CDATA?

Scenario: There's a news site; the headline, breaking news, exchange rates, sports results need to be updated instantly. How can we update using XML?

First, let's list the areas that need to be updated on the site:

  • Site Title
  • Breaking News
  • Headline
  • Exchange Rates
  • Sports Results

Let's create an XML suitable for this structure.

<?xml version="1.0" encoding="UTF-8"?>
<site update="01.01.2015 12:15">
	<head>
		<title></title>
	</head>
	<body>
		<headline></headline>
		<breaking></breaking>
		<currency></currency>
		<sports></sports>
	</body>
</site>

After creating the tree structure above, if we add HTML codes along with filling in the required data in the headline section, we'll get an error.

We can solve this with CDATA.

<?xml version="1.0" encoding="UTF-8"?>
<site update="01.01.2015 12:15">
	<head>
		<title>Breaking News - News</title>
	</head>
	<body>
		<headline>
			<![CDATA[
			<li><img src="firstcar.jpg"></li>
			<li><img src="firstplane.jpg"></li>
			<li><img src="firstsatellite.jpg"></li>
			]]>
		</headline>
		<breaking>
			<![CDATA[
			<li>The first national vehicle powered by boron was produced.</li>
			<li>The first national aircraft was tested by the prime minister.</li>
			<li>The first national satellite was launched by TUBITAK from Kirsehir.</li>
			]]>
		</breaking>
		<currency>
			<![CDATA[
			<div class="dollar">
				x.xxxx
			</div>
			<div class="euro">
				x.xxxx
			</div>
			<div class="bist">
				x.xxxx
			</div>
			]]>
		</currency>
		<sports>
			<![CDATA[
			<li>Incredibly talented kid.</li>
			<li>Shake-up in Galatasaray management.</li>
			<li>Shock transfer to Fenerbahce.</li>
			<li>Sponsorship deal from Besiktas.</li>
			]]>
		</sports>
	</body>
</site>

During update operations, you can quickly complete the update by parsing the XML data.