转义 XML 中的双引号字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1222367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Escape double quote character in XML
提问by ufukgun
Is there an escape character for a double quote in xml? I want to write a tag like:
xml 中的双引号是否有转义字符?我想写一个标签,如:
<parameter name="Quote = " ">
but if I put ", then that means string has ended. I need something like this (c++):
但是如果我放了“,那么这意味着字符串已经结束。我需要这样的东西(c++):
printf("Quote = \" ");
Is there a character to write before the double quote to escape it?
在双引号之前是否有一个字符要转义它?
回答by Andrew Hare
Try this:
尝试这个:
"
回答by Alex Muriithi
Here are the common characters which need to be escaped in XML, starting with double quotes:
以下是需要在 XML 中转义的常见字符,以双引号开头:
- double quotes (
") are escaped to" - ampersand (
&) is escaped to& - single quotes (
') are escaped to' - less than (
<) is escaped to< - greater than (
>) is escaped to>
- 双引号 (
") 被转义为" - &符号 (
&) 被转义为& - 单引号 (
') 被转义为' - 小于 (
<) 被转义为< - 大于 (
>) 被转义为>
回答by Jon Skeet
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 API - 几乎所有现有的现代编程平台都有很多可用的。
XML APIs will handle things like this for you automatically, making it a lotharder to go wrong. Unless you're writing an XML API yourself, you should rarely need to worry about the details like this.
XML的API将自动处理这样的事情对你来说,使它成为很多困难出问题。除非您自己编写 XML API,否则您应该很少需要担心这样的细节。
回答by kjhughes
New, improved answer to an old, frequently asked question...
对一个旧的、常见的问题的新的、改进的答案......
When to escape double quote in XML
何时在 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"/>
在 XML 文本内容中:
<NoEscapeNeeded>He said, "Don't quote me."</NoEscapeNeeded>在由单引号 (
')分隔的 XML 属性中:<NoEscapeNeeded name='Pete "Maverick" Mitchell'/>注意:切换到单引号 (
') 也不需要转义:<NoEscapeNeeded name="Pete 'Maverick' Mitchell"/>
Double quote (") must be escaped:
双引号 ( ")必须转义:
In XML attributes delimited by double quotes:
<EscapeNeeded name="Pete "Maverick" Mitchell"/>
在由双引号分隔的 XML 属性中:
<EscapeNeeded name="Pete "Maverick" Mitchell"/>
Bottom line
底线
Double quote (") must be escaped as "in XML only in very limited contexts.
双引号 ( ") 必须像"在 XML 中一样仅在非常有限的上下文中进行转义。
回答by Matt Howells
No there isn't an escape character as such, instead you can use "or even <![CDATA["]]>to represent the "character.
不,没有这样的转义字符,相反,您可以使用"甚至<![CDATA["]]>来表示该"字符。
回答by Brad Cupit
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 = " '>
回答by Neven
In C++ you can use EscapeXML ATL API. This is the correct way of handling special chars ...
在 C++ 中,您可以使用 EscapeXML ATL API。这是处理特殊字符的正确方法......
回答by E-max
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”,然后是字符的 unicode 值,例如双引号的 unicode 值是
" -> U+0022
" -> U+0022
Therefore if you were setting it as part of text in XML in android it would look something like this,
因此,如果您将其设置为 android 中 XML 文本的一部分,它将看起来像这样,
<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
这将在 TextView 中产生一个大致像这样的文本
" Showing double quotes "
" 显示双引号 "
You can find unicode of most symbols and characters here www.unicode-table.com/en
您可以在这里找到大多数符号和字符的 unicode www.unicode-table.com/en

