2013-02-17 30 views
11

Tôi có một nguồn tài nguyên chuỗi trong dự án Android của tôi, và tôi đang sử dụng một phần CDATA để nhúng chuỗi của tôi trong XML:Lỗi! sử dụng CDATA trong tài nguyên 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> 

nhưng eclispe đang trở lại báo lỗi này:

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

Trả lời

12

Dấu nháy đơn (') phải được thoát bằng cách sử dụng \. Hãy thử sử dụng \' thay vì ' trong toàn bộ nội dung của Chuỗi của bạn.

Ngoài ra, bạn sẽ cần phải thoát khỏi " ở phần sau đây với \":

“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

Sử dụng &#39; thay vì dấu nháy đơn. Hiệu ứng của ký tự là trong trường hợp đầu ra HTML (Tôi đoán nó là html) bằng nhau. Vì vậy, &#39; sẽ được hiển thị là '.

+0

Tôi không biết ai đã bỏ phiếu này, nhưng điều này * là * chính xác ... +1 – Ahmad

8

String Formatting and Styling nói

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> 

Dường như bad_example_2 mô tả vấn đề của bạn. Đặt cược tốt nhất của bạn là bắt chước good_example_2 bằng cách thay thế cả hai số '" với \'\" tương ứng theo đề xuất của Raghav Sood.

+1

tôi nghĩ rằng việc sử dụng để giữ mã html cho khả năng miễn dịch chống lại các vấn đề như apostrophe –

+1

@mister_dani, hiểu biết của tôi là Phần CDATA ảnh hưởng đến cách trình phân tích cú pháp XML lấy ra một nút văn bản từ một chuỗi XML, nhưng tài liệu đó chỉ ra rằng bộ xử lý tài liệu XML mong đợi các nút văn bản (sau khi phân tích cú pháp) sử dụng một quy ước thoát riêng biệt với thoát XML. –

+0

@mister_dani no, unintuitive của nó nhưng CDATA không giúp đỡ với dấu nháy đơn, bạn cũng cần phải thoát khỏi nó. – for3st

Các vấn đề liên quan