【问题标题】:How to escape the < and ≤ symbols in xml?如何转义 xml 中的 < 和 ≤ 符号?
【发布时间】:2013-12-13 12:41:33
【问题描述】:

我想在android中创建一个字符串&lt;res&gt;,如下所示:

<string name="bmi0">0: BMI≤18.5</string>
<string name="bmi1">1: 18.5<BMI≤24</string>
<string name="bmi2">2: 24<BMI≤27</string>

但我显示了“标签启动未关闭”的错误。

我试图在前面放一个\ 符号,但它不起作用,它显示了同样的错误。

<string name="bmi0">0: BMI\≤18.5</string>
<string name="bmi1">1: 18.5<BMI\≤24</string>
<string name="bmi2">2: 24<BMI\≤27</string>

如何转义特殊的 XML 符号?

【问题讨论】:

  • 谢谢大家!! =D

标签: java android xml string escaping


【解决方案1】:

您必须按以下方式转义这些符号:

  • &amp;lt; 将是 &amp;lt;
  • &amp;gt; 将是 &amp;gt;

符号不需要转义。

【讨论】:

  • 这不是完全忽略了正在使用的“小于或等于”符号的点吗,≤?
  • @peter.petrov 啊,我猜OP的错误只是因为使用了常规的<.>
  • 我想我首先回答了这个问题 btw :) @kocko 这不是 ≤实际上
  • 它显示“未转义 & 或非终止字符/实体引用”错误。当我使用“&lt”
  • 您必须在其后添加一个冒号 (;)。
【解决方案2】:

您必须为 XML 转义这些符号。

What characters do I need to escape in XML documents?

【讨论】:

    【解决方案3】:

    如前所述,对于&lt;&gt;,您可以使用:

    <   &lt;
    >   &gt;
    

    您需要这样做,因为 XML 中不允许使用 &lt;&gt;,因此您需要 escape 它们。您可以通过键入:&amp; + specific code for your character + ; 来做到这一点,当您显示此字符串时,这将被识别为特殊字符(&lt;&gt;)。

    您还需要 smaller or equal 符号 (≤),这是 XML 中允许的 afaik,所以只需使用

    这些是你必须转义的字符:

    "   =   &quot;
    '   =   &apos;
    <   =   &lt;
    >   =   &gt;
    &   =   &amp;
    

    【讨论】:

      【解决方案4】:

      使用这个:

      <   &lt;
      >   &gt;
      

      【讨论】:

        【解决方案5】:

        使用

        https://stackoverflow.com/a/4301267/2740014

        转义字符串,然后将它们放入您的 xml。

        StringEscapeUtils.escapeXml(stringHere);
        

        是我认为需要的东西。

        【讨论】:

          【解决方案6】:

          使用统一码

          对于&lt;,请使用\u003c
          对于&gt; \u003e
          对于\u2264

          要获取任何字符串的 unicode,请使用以下方法

          public static String getUnicode(String input){
              String output = "";
              for(char c:input.toCharArray()){
                  output +="\\u" + Integer.toHexString(c|0x10000).substring(1) ;
              }
              return output;
          }
          

          【讨论】:

          • 为什么它有+1? \uxxxx 不适用于 xml
          • @Selvin 这是标记为 android 的,在 strings.xml 中它 100% 有效,而且它是使用一些特殊字符的唯一方法,例如“非破坏连字符”\uXXXX 甚至被建议LINT(例如,如果您键入“...”)
          • 确定唯一的方法....&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;root&gt;&lt;test name="&amp;#8209;"/&gt;&lt;/root&gt; xml 表示法是 XXX;或 HHHH;
          • @selvin 是的,你是对的(而且我刚刚看到,lint 不再建议它了)但是,如果它不是用于 xml,为什么它仍然有效?
          • 来自 The Source w3.org/TR/REC-xml/#dt-charref ... 没有一个关于 \u 表示法的词
          【解决方案7】:

          对特殊字符使用 Unicode 序列。

          您可以在这里找到其中的一些:http://jrgraphix.net/r/Unicode/2200-22FF

          像这样使用它:

          \u2264
          

          将转换为

          【讨论】:

          • 为什么它有+1? \uxxxx 不适用于 xml
          【解决方案8】:

          只需我的 2 美分:番石榴 15 引入了 Escapers,特别是针对这种情况:

           Escaper escaper = XmlEscapers.xmlAttributeEscaper();
           String result = escaper.escape("<>");
           System.out.println(result); // &lt;&gt;
          

          【讨论】:

            猜你喜欢
            • 2018-12-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-15
            • 2011-11-20
            • 1970-01-01
            • 1970-01-01
            • 2023-03-07
            相关资源
            最近更新 更多