AHmedRef
AHmedRef

Reputation: 2621

decoding/encoding of special characters in XML

I want to generate an xml page in php but I found the problem at decoding / encoding special characters for example ""é""

myCode :

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
 <url>
    <loc> &eacute; test &egrave; </loc>
    <priority>1.000</priority>
    <changefreq>always</changefreq>
 </url>
</urlset>

but i get this message

This page contains the following errors:

error on line 4 at column 19: Entity 'eacute' not defined
Below is a rendering of the page up to the first error.

Upvotes: 1

Views: 3413

Answers (2)

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46778

The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section.

(Reference)

Use this instead

<loc>&amp;eacute; test &amp;egrave;</loc>

The & must be encoded as &amp;


OR use it within a CData section. It says that the following text isn't markup, but it character data instead.

<loc><![CDATA[&eacute; test &egrave;]]></loc>

Upvotes: 1

wberry
wberry

Reputation: 19377

Do you mean you want to put HTML entities in your XML or literal characters in your XML?

The only entities defined by XML are:

  • &amp;
  • &lt;
  • &gt;
  • &quot;
  • &apos;

Nothing else. And only the first two are strictly necessary to represent any Unicode text in an XML document. It's perfectly OK to include literal >, ' and " in XML text.

To include literal characters in your XML document, just put them in there, and ensure that the encoding you use to produce the document in bytes format is the same as what is in the declaration. There are certain character classes that must be respected in element and attribute names, and you must still remember to use the &amp; and &lt; entities instead of the literals.

http://www.w3schools.com/xml/xml_encoding.asp

Upvotes: 1

Related Questions