错误!在strings.xml资源Android中使用CDATA

时间:2013-02-17 23:22:57

标签: android cdata

我的Android项目中有一个字符串资源,我使用CDATA部分将我的字符串嵌入XML:

<string name="ackng"><![CDATA[
    <html>
<body>
    <h1>Acknowledgements</h1> 
    <p>
        Senator  Paul  Wellstone's  book,  the  Conscience  of  a  Liberal:  Reclaiming  the
        compassionate agenda was an inspiration and an affirmation that you can be truthful
        and  honest  as  a  politician,  and  compassionate  and  people-centred  as  a  representative.
        And  as  President  Bill  Clinton  affirmed  as  he  addressed  the  joint  session  of  the
        National  Assembly  on  26th  August,  2000,  thus:
        “Every  nation  that  has  struggled  to  build  democracy  has  found  that  success
        depends on leaders  who believe government exists to  serve people, not the
        other  way  around.”
    </p>
    <p>
        Paul  Wellstone's  book  was  my  inspiration  for  the  launching  of  the  compassionate
        agenda  in  February  2002.  Clinton's  speech  affirmed  my  convictions  regarding  the
        sanctity of the democratic mandate to serve the people. I acknowledge the clarity of
        their  focus  and  the  inspiration  their  works  continue  to  provide.
    </p>
</body></html>]]></string>

但是eclispe正在返回此错误:

[2013-02-18 00:11:05 - MyPA] C:\Users\Daniel\workspace\MyPA\res\values\strings.xml:191: error: Apostrophe not preceded by \ (in  <html>

3 个答案:

答案 0 :(得分:12)

必须使用'转义撇号(\)。尝试在字符串的整个内容中使用\'代替'

此外,您需要使用"在以下部分中转义\"

“Every  nation  that  has  struggled  to  build  democracy  has  found  that  success
depends on leaders  who believe government exists to  serve people, not the
other  way  around.”

答案 1 :(得分:8)

使用&#39;代替撇号。在HTML输出(我猜它是html)相等的情况下,字符的效果是。因此&#39;将显示为'

答案 2 :(得分:8)

String Formatting and Styling

  

转义撇号和引号

     

如果字符串中有撇号或引号,则必须将其转义或将整个字符串括在其他类型的引号中。例如,以下是一些有效和无效的叮咬:

<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>

看起来bad_example_2描述了您的问题。您最好的选择是模仿good_example_2,将Raghav Sood建议的'"分别替换为\'\"