Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error! using CDATA in strings.xml resource Android

Tags:

android

cdata

I have a string resource in my Android project, and I am using a CDATA section to embed my string in 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>

but eclispe is returning this error:

[2013-02-18 00:11:05 - MyPA] C:\Users\Daniel\workspace\MyPA\res\values\strings.xml:191: error: Apostrophe not preceded by \ (in  <html>
like image 769
letroot Avatar asked Feb 17 '13 23:02

letroot


3 Answers

Apostrophes (') must be escaped using \. Try using \' instead of ' throughout the content of your String.

Also, you'll need to escape " in the following part with \":

“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.”
like image 123
Raghav Sood Avatar answered Oct 29 '22 12:10

Raghav Sood


String Formatting and Styling says

Escaping apostrophes and quotes

If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don't work:

<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>

It looks like bad_example_2 describes your problem. Your best bet is to mimic good_example_2 by replacing both ' and " with \' and \" respectively as suggested by Raghav Sood.

like image 8
Mike Samuel Avatar answered Oct 29 '22 14:10

Mike Samuel


Use &#39; instead of apostrophe. The effect of character is in case of HTML output (I guess it is html) equal. So &#39; will be displayed as '.

like image 8
Stepo Avatar answered Oct 29 '22 13:10

Stepo