2013-02-17 14 views
11

Ho una risorsa di stringa nel mio progetto Android, e sto usando una sezione CDATA per incorporare la mia stringa in XML:Errore! utilizzando CDATA in risorsa 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> 

ma eclispe sta tornando questo errore:

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

risposta

12

Apostrofi (') devono essere preceduti da escape utilizzando \. Prova a utilizzare \' anziché a ' in tutto il contenuto della stringa.

Inoltre, avrete bisogno di fuggire " nel seguito con \":

“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

Uso &#39; invece di apostrofo. L'effetto del carattere è in caso di output HTML (suppongo che sia html) uguale. Quindi &#39; verrà visualizzato come '.

+0

Io non so chi ha votato giù questo, ma questo è * * corretto ... +1 – Ahmad

8

String Formatting and Styling dice

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> 

Sembra bad_example_2 descrive il problema. La soluzione migliore è imitare good_example_2 sostituendo sia ' e " con \' e \" rispettivamente come suggerito da Raghav Sood.

+1

penso utilizzando per contenere codice html dargli l'immunità contro tali problemi come apostrofo –

+1

@mister_dani, la mia comprensione è che il La sezione CDATA influenza il modo in cui il parser XML deriva un nodo di testo da una stringa di XML, ma quella documentazione indica che il processore di documenti XML si aspetta che i nodi di testo (dopo l'analisi) utilizzino una particolare convenzione di escape separata dall'escaping XML. –

+0

@mister_dani no, non è intuitivo ma CDATA non aiuta con l'apostrofo, devi anche scappare. – for3st

Problemi correlati