blackuprise
blackuprise

Reputation: 450

XML documents and & char?

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

Upvotes: 2

Views: 3478

Answers (3)

lavinio
lavinio

Reputation: 24319

&, < 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.)

Upvotes: 4

Eric J.
Eric J.

Reputation: 150198

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.

Upvotes: 2

Amber
Amber

Reputation: 527328

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

Upvotes: 8

Related Questions