tags:

views:

326

answers:

4

Hi,

Is there any function that I can use to parse any string to ensure it won't cause xml parsing problems? I have a php script outputting a xml file with content obtained from forms.

The thing is, apart from the usual string checks from a php form, some of the user text causes xml parsing errors. I'm facing this "’" in particular. This is the error I'm getting Entity 'rsquo' not defined

Does anyone have any experience in encoding text for xml output?

Thank you!


Some clarification: I'm outputting content from forms in a xml file, which is subsequently parsed by javascript.

I process all form inputs with: htmlentities(trim($_POST['content']), ENT_QUOTES, 'UTF-8');

When I want to output this content into a xml file, how should I encode it such that it won't throw up xml parsing errors?

So far the following 2 solutions work:

1) echo '<content><![CDATA['.$content.']]></content>';

2) echo '<content>'.htmlspecialchars(html_entity_decode($content, ENT_QUOTES, 'UTF-8'),ENT_QUOTES, 'UTF-8').'</content>'."\n";

Are the above 2 solutions safe? Which is better?

Thanks, sorry for not providing this information earlier.

+1  A: 
html_entity_decode($string, , ENT_QUOTES, 'UTF-8')
sleepynate
Lyon
@Lyon: Maybe you should show us what you’re actually doing.
Gumbo
it really depends on your output format. are you outputting in utf-8? does your xml output start with something like: `header("Content-Type: text/html;charset=ISO-8859-1");` `print "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";`
sleepynate
yep. i'm outputting in utf-8. my xml output starts with `echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";`thanks
Lyon
+3  A: 

You take it the wrong way - don't look for a parser which doesn't give you errors. Instead try to have a well-formed xml.

How did you get &rsquo; from the user? If he literally typed it in, you are not processing the input correctly - for example you should escape & to &amp;. If it is you who put the entity there (perhaps in place of some apostrophe), either define it in DTD (<!ENTITY rsquo "&x2019;">) or write it using a numeric notation (&#x2019;), because almost every of the named entities are a part of HTML. XML defines only a few basic ones, as Gumbo pointed out.

EDIT based on additions to the question:

  • In #1, you escape the content in the way that if user types in ]]> <°)))><, you have a problem.
  • In #2, you are doing the encoding and decoding which result in the original value of the $content. the decoding should not be necessary (if you don't expect users to post values like &amp; which should be interpreted like &).

If you use htmlspecialchars() with ENT_QUOTES, it should be ok, but see how Drupal does it.

Krab
Lyon
Is there any reason you must use htmlentities() and not htmlspecialchars()?
Krab
no particular reason actually..would htmlspecialchars() suffice to process all user inputs? when would one use htmlentities() then?
Lyon
so actually, if i'm already processing, saving and outputting inputs all in utf-8, i wouldn't need htmlentities, and htmlspecialchars would suffice? Thanks!
Lyon
Maybe you could use htmlentities() to produce a document containing unicode characters while preserving plain ASCII encoding. But this is only true if you it also encodes characters to numeric entities. I don't know.
Krab
thanks Krab. really appreciate your help in this. I've decided to change htmlentities to htmlspecialchars thus negating the need to decode/encode any content before outputting to xml. thanks! :)
Lyon
+1  A: 

Enclose the value within CDATA tags.

<message><![CDATA[&rsquo;]]></message>

From the w3schools site:

Characters like "<" and "&" are illegal in XML elements.

"<" will generate an error because the parser interprets it as the start of a new element.

"&" will generate an error because the parser interprets it as the start of an character entity.

Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.

Everything inside a CDATA section is ignored by the parser.

Joseph
this is a good solution, though I shudder at unescaped CDATA being used somewhere else in the code further down the line (for example, to be put in to a database), though that's not directly related to the problem at hand.
sleepynate
Hello world!]]>
Porges
+2  A: 

The problem is that your htmlentities function is doing what it should - generating HTML entities from characters. You're then inserting these into an XML document which doesn't have the HTML entities defined (things like &rsquo; are HTML-specific).

The easiest way to handle this is keep all input raw (i.e. don't parse with htmlentities), then generate your XML using PHP's XML functions.

This will ensure that all text is properly encoded, and your XML is well-formed.

Example:

$user_input = "...<>&'";

$doc = new DOMDocument('1.0','utf-8');

$element = $doc->createElement("content");
$element->appendChild($doc->createTextNode($user_input));

$doc->appendChild($element);
Porges
thanks Porges! Currently i'm just echo-ing out into a xml file. I'll use PHP's XML functions to properly produce a xml document. :)
Lyon