views:

212

answers:

3

Hi Girls and rest,

I have a question for XML document special chars, I'm using & in on of the value of the item in XML and TXMLDoc Delphi parser is complaining about it.

I search for some XML parsing options but none of them concerning special chars,

any ideas?

Example:

    <Configuration>
      <Configuration_item>  
        <view_name value="some view & name"/>

....

Regards, Mika

+7  A: 

Use &amp; to represent an ampersand. The reason you need to do this is because & is a special character in XML - specifically, the character used to indicate the start of an XML entity, which is used to specify characters in the content that would (ironically) otherwise be special/complex characters.

<view_name value="some view &amp; name"/>

Other common XML entities:

  • " is &quot;
  • < is &lt; (short for less-than)
  • > is &gt; (short for greater-than)

For more info:

http://www.xml.com/pub/a/2001/01/31/qanda.html

Amber
wow man, that was quick, I was still editing post, and correct! Thanks!
+3  A: 

&, < and > are the special characters you'll want to watch out for, and within an attribute also ". & is kind of like the XML escape character.

They can be escaped thus:

  • & => &amp;
  • < => &lt;
  • > => &gt;
  • " => &quot;

(Actually, and oddly, you don't really need to escape > within an attribute, only <. Also, if you use ' instead of " for your attribute delimiters, instead of having to escape " as &quot; you need to escape ' as &apos;. See the AttValue reference in the XML specification for details.)

lavinio
+2  A: 

Dav and lavinio are right.

If you have a block of text with lots of special characters you might want to enclose it in CDATA instead.

Eric J.
NOTE: You can't use CDATA for attribute values, just for blocks of text between tags.
Eric J.