We help IT Professionals succeed at work.

Special characters in attributes

I am reading/writing XML using DOM from C++.  Some attributes are a C-like expression that can include &, <, > as well as single- and/or double-quotes.

I have found lots of info on XML attributes that basically says if you want to use a double-quote enclose the attribute in single-quotes and vice-versa.  None address the issue of including both double- and single-quotes in the same attribute text.

Also, I assume that I have to converts and ampersand (&) to &amp; when writing the XML, and convert back when reading.  Putting an & in the attribute text does not work, although I have never read that it should be a problem.
Watch Question

Independent Consultant

Basically any reserved character according to the XML spec. has to be escaped (or quoted) with the &...; sequence you mentioned.

See 2.4 of the spec for details:


& becomes &amp;
' becomes &apos;
" becomes &quot;


If you're using the DOM, the conversions should be transparent -- you shouldn't have to escape text strings manually.



wbradney said:
> & becomes &amp;
> ' becomes &apos;
> " becomes &quot;
Thanks, I have never seen &apos; or &quot; before.  I also didn't know you could use &amp; in an attribute string.

> If you're using the DOM, the conversions should be transparent ...

You are right.  I was creating the XML by hand for testing.  The & was giving errors when reading the attributes but using DOM to create the XML this all becomes transparent.  

Explore More ContentExplore courses, solutions, and other research materials related to this topic.