Home Coding Spry with HTML codes/CDATA in XML

Spry with HTML codes/CDATA in XML

by Ben

I was assigned with a mini project to do a team notice board that read XML files using Spry.
While reading plain text in the XML is straightforward,  reading of HTML codes like <br> tag, <b> tag are not.

<?xml version="1.0" encoding="utf-8"?>
<posts>
	<post>
		<msg>This is a <strong>message</strong></msg>
	</post>
</posts>
<msg>This is a <strong>message</strong></msg>

When you type these tags(e.g. strong tag as above) in the XML, it simply ‘breaks’ the XML formatting and would thus caused an error when it is loaded with Spry

So… to input HTML data in XML…

1. Use CDATA wrapper in your XML

		<msg>
			<![CDATA[
			This is a <strong>message</strong>
			]]>
		</msg>

2. Set setColumnType(“msg”, “html”) to your Spry dataset for that field

var ds1 = new Spry.Data.XMLDataSet("data.xml", "posts/post");
ds1.setColumnType("msg", "html");

Spry with HTML Data Source Code

Spry with HTML Data Online Demo

You may also like

Leave a Comment