Ошибка! использование CDATA в файле strings.xml Android
У меня есть строковый ресурс в моем проекте Android, и я использую раздел CDATA для вставки моей строки в XML:
<string name="ackng"><![CDATA[
<html>
<body>
<h1>Acknowledgements</h1>
<p>
Senator Paul Wellstone 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 book was my inspiration for the launching of the compassionate
agenda in February 2002. Clinton 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>
Ответы
Ответ 1
Апострофы ('
) должны быть экранированы с помощью \
. Попробуйте использовать \'
вместо '
во всем содержимом вашей строки.
Кроме того, вам нужно выйти "
в следующей части с помощью \"
:
"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."
Ответ 2
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't work</string>
Похоже, что bad_example_2
описывает вашу проблему. Лучше всего подражать good_example_2
, заменив как '
, так и "
на \'
и \"
соответственно, как предложено Raghav Sood.
Ответ 3
Используйте '
вместо апострофа. Эффект характера в случае вывода HTML (я думаю, это html) равен. Поэтому '
будет отображаться как '
.