XML标记中的<![CDATA []>

I have an XML file that is parsed with a PHP file. I have to include a lot of "Special" characters which need CDATA in order to parse correctly.

Is there a way to tell my PHP file to read all the tags as if there was a block at the begging and and of the tag?

As of right now for every XML tag a create i have to put a CDATA block:

<tag><![CDATA[blah.......]]></tag>

Is there a way to set it up where I don't have to write CDATA every time for evey tag in my XML?

CDATA is a bad idea! There's a number of problems with it. What you should do instead, is use htmlspecialchars() for every value.

Alright.. Hold your downvotes! Here are some issues with CDATA.

First, the easy one: You cannot escape the ]]> sequence. This may not seem like a huge deal, but if you are picking any method for 'escaping character sequences', you really should pick one where every single sequence is escapable.

Now for the big one: CDATA is often used as a hack to inject Latin1 data into a UTF-8 document. People figure, I have an escaping problem in XML, so I will use CDATA as a workaround.

In CDATA any character sequence is allowed, and the specified character encoding of the XML document is no longer relevant in this block. However, any type of text actually does have a character encoding, and instead of convering the encoding (what you should do) you 'hack' around this by wrapping it in CDATA.

It is also not a viable way to encode binary data, as control-characters are still not allowed.

So, CDATA kind of implies 'here be dragons', there are bytes here that are not in a specified encoding, all I can tell you there are no control characters.

This is a bad idea for the consumer, because all assumptions about character encoding is now gone.

Here are some links:

You haven't told us specifically what "special characters" you're referring to, but I'm assuming you mean some kind of accented characters, or characters in a non-latin alphabet, etc?

In most cases the problem can be solved by outputting the document using the UTF-8 character set.

In the remaining cases, it can be solved by using XML entities -- eg &#160;.

Both of these are better solutions than using CDATA.