2013-02-17 8 views
11

Mam zasobu ciąg w moim Android projektu i używam sekcji CDATA osadzić mój ciąg w xml:Błąd! przy użyciu CDATA w zasobie strings.xml Android

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

ale eclispe wraca ten błąd:

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

Odpowiedz

12

Apostrofy (') należy uciec przy użyciu \. Spróbuj użyć \' zamiast ' w całej treści Twojego ciągu znaków.

Ponadto, trzeba uciec " w dalszej części z \":

“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.” 
7

Zastosowanie &#39; zamiast apostrofu. Efektem działania jest w przypadku wyjścia HTML (domyślam się, że jest to html). Tak więc &#39; zostanie wyświetlony jako '.

+0

Nie wiem, kto to głosował, ale to * jest * poprawne ... +1 – Ahmad

8

String Formatting and Styling mówi

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> 

Wygląda bad_example_2 opisuje swój problem. Najlepiej jest naśladować good_example_2, zastępując odpowiednio ' i " z \' i \" zgodnie z sugestią Raghava Sooda.

+1

Myślę, że używanie do przechowywania kodu html daje mu odporność na takie problemy jak apostrof –

+1

@mister_dani, rozumiem, że Sekcja CDATA wpływa na sposób, w jaki parser XML wyprowadza węzeł tekstowy z łańcucha XML, ale ta dokumentacja wskazuje, że procesor dokumentów XML oczekuje, że węzły tekstowe (po analizie składniowej) wykorzystają określoną konwencję unikania oddzieloną od wychodzenia XML. –

+0

@mister_dani nie, jego nieintuicyjne, ale CDATA nie pomaga w apostrofach, musisz także uciec. – for3st