2013-02-17 31 views
11

我在我的Android項目的字符串資源,我使用的是CDATA部分嵌入我的字符串中的XML:錯誤!在strings.xml中使用資源的CDATA的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> 

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

回答

12

撇號(')必須使用\進行轉義。在整個String的內容中嘗試使用\'而不是'

此外,你需要逃避"在下面的部分用\":的

「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

使用&#39;代替省略號。字符的效果是在HTML輸出的情況下(我猜是html)相等。因此&#39;將顯示爲'

+0

我不知道誰投下了這個,但這*是*正確的... +1 – Ahmad 2013-02-17 23:42:24

8

String Formatting and Styling

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> 

它看起來像bad_example_2描述問題。最好的辦法是模仿good_example_2,分別將'"分別替換爲\'\",正如Raghav Sood所建議的那樣。

+1

我認爲使用來保存html代碼給它免疫這樣的問題,如撇號 – 2013-02-19 12:48:55

+1

@mister_dani,我的理解是, CDATA部分影響XML解析器如何從XML字符串派生文本節點,但該文檔指出XML文檔處理器期望文本節點(解析後)使用與XML轉義分開的特定轉義約定。 – 2013-02-19 16:40:41

+0

@mister_dani不,它的不直觀,但CDATA不幫助撇號,你也需要逃避它。 – for3st 2014-05-05 15:08:58

相關問題