122

Is there an escape character for a double quote in xml? I want to write a tag like:

<parameter name="Quote = " ">

but if I put ", then that means string has ended. I need something like this (c++):

printf("Quote = \" ");

Is there a character to write before the double quote to escape it?

4

8 Answers 8

207

Try this:

&quot;
144

Here are the common characters which need to be escaped in XML, starting with double quotes:

  1. double quotes (") are escaped to &quot;
  2. ampersand (&) is escaped to &amp;
  3. single quotes (') are escaped to &apos;
  4. less than (<) is escaped to &lt;
  5. greater than (>) is escaped to &gt;
5
  • 6
    Incorrect; &apos; is not a valid XML character. It is a valid HTML character - which is SGML and a SUPERset of XML. Dec 8, 2017 at 8:05
  • While that's a valid point, most modern XML parsers I've worked with are accepting it as valid character. Dec 15, 2017 at 2:22
  • &quot; is not correctly interpreted by Android when the string is surrounded with double quotes. In this case E-max answer is the only one to work.
    – Softlion
    Apr 1, 2018 at 6:10
  • @StefanSteiger it's on this list
    – OrangeDog
    Sep 5, 2018 at 14:25
  • 1
    @Dariusz G. Jagielski: While I have nothing against modern XML parsers - if you work with XML in a database (e.g. ms/pg), you'll see that not every piece of software is using modern xml parsers. Especially when it's complex decades old software. That said, if everyone used a modern xml parser, it would be a good thing. However, if that code is deeply integrated in older complex software, it gets very hard to replace it (while retaining 100% backwards compatiblity). The downsides of C or of not using dependency injection, so to say. Sep 5, 2018 at 18:26
28

Others have answered in terms of how to handle the specific escaping in this case.

A broader answer is not to try to do it yourself. Use an XML API - there are plenty available for just about every modern programming platform in existence.

XML APIs will handle things like this for you automatically, making it a lot harder to go wrong. Unless you're writing an XML API yourself, you should rarely need to worry about the details like this.

3
  • 12
    I can't say that I agree. It seems foolish to think that we should be so reliant on API's to abstract us to the point that we don't even need to understand how to read and write XML. Tha XML API probably isn't going to help you a lot when you feed it a malformed document and it blows up. Someone is going to have to fix the data to get it to load. Apr 5, 2012 at 13:05
  • @ChristopherPainter Given the size of the XML standard, I doubt that there are very many people who do actually understand it. Now there are many who think that they do, but they really don't. Aug 19, 2015 at 19:37
  • 2
    Well, you got to start somewhere. Course in our industry we just complain that XML sucks and start over with something else like JSON. Then the process repeats. Aug 19, 2015 at 19:40
18

New, improved answer to an old, frequently asked question...

When to escape double quote in XML

Double quote (") may appear without escaping:

  • In XML textual content:

    <NoEscapeNeeded>He said, "Don't quote me."</NoEscapeNeeded>
    
  • In XML attributes delimited by single quotes ('):

    <NoEscapeNeeded name='Pete "Maverick" Mitchell'/>
    

    Note: switching to single quotes (') also requires no escaping:

    <NoEscapeNeeded name="Pete 'Maverick' Mitchell"/>
    

Double quote (") must be escaped:

  • In XML attributes delimited by double quotes:

    <EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell"/>
    

Bottom line

Double quote (") must be escaped as &quot; in XML only in very limited contexts.

1
  • The information here is especially useful in HTML, where I found browsers may render an encoded quote literally as &quot; where a quote was encoded when it wasn't needed.
    – ecnepsnai
    Mar 26 at 20:00
8

If you just need to try something out quickly, here's a quick and dirty solution. Use single quotes for the attribute value:

<parameter name='Quote = " '>
1
  • I wouldn't call it a quick an dirty solution, it's rather elegant and easy. :)
    – harre
    Sep 22, 2023 at 11:35
7

No there isn't an escape character as such, instead you can use &quot; or even <![CDATA["]]> to represent the " character.

2

In C++ you can use EscapeXML ATL API. This is the correct way of handling special chars ...

2

You can try using the a backslash followed by a "u" and then the unicode value for the character, for example the unicode value of the double quote is

" -> U+0022

Therefore if you were setting it as part of text in XML in android it would look something like this,

<TextView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:text=" \u0022 Showing double quotes \u0022 "/>

This would produce a text in the TextView roughly something like this

" Showing double quotes "

You can find unicode of most symbols and characters here www.unicode-table.com/en

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.