2015-02-11 25 views
2

Please see the difference in the degree symbol請參閱下面提供的代碼。使用Delphi XE7中的TXML Document對象在XML文件中無法正確顯示度符號

****XMLDoc.Active := True; 
    XMLDoc.Options := [doNodeAutoCreate, doNodeAutoIndent]; 
    XMLDoc.Version := '1.0'; 
    XMLDoc.Encoding := 'utf-8';**** 

?xml version =「1.0」encoding =「UTF-8」?這是預計結果當我打開XML文件。但是它沒有在XML文件中顯示編碼=「UTF-8」。正因爲如此,我認爲度數符號沒有正確顯示在XML文件中(因爲它無法進行編碼)。

我將UTF-8更改爲UTF-16,但是當我試圖打開XML文件時,它顯示了錯誤消息,如從當前編碼切換到指定編碼不支持。我猜在RAD Studio XE7中存在一些編碼問題,因爲對於以前的版本(Delphi XE5),它工作正常。請給我一些建議。

下面我提供了示例代碼。

XMLDocument1:= TXMLDocument.create(nil); 
    XMLDocument1.Active := True; 
    XMLDocument1.Version :='1.0'; 
    XMLDocument1.Encoding :='UTF-8'; 
     XMLDocument2:= TXMLDocument.create(nil); 
    XMLDocument2.Active := True; 
    XMLDocument2.Version :='1.0'; 
    XMLDocument2.Encoding :='UTF-8';   

    { Add new child. This will become the document element. 
    If the document element already exists, then an exception is raised. } 
    //LNode := XMLDocument1.AddChild('Airbus'); 
    LNode := XMLDocument1.CreateElement('TestElement', '101°F (38.33°C)'); 
    XMLDocument1.DocumentElement := LNode; 

    { Display document content. } 

    XMLDocument1.SaveToFile('c:\mk1.xml'); 
{if we take xmldocuments.xml.text method is not taking 「Encoding=’UTF-8’} 
    XMLDocument2.LoadFromFile ('c:\mk1.xml'); 
    **strsampletext:= XMLDocument2.XML.text; //Here I am not getting "Encoding='UTF-8". and the string "strsampletext" passed as a parameter**. If I write "strsampletext" into another XML file I can't view Degree Symbol correctly when I view in IE. 

加載保存的XML後,編碼後的UTF-8未採用。

+0

你可以顯示代碼寫入XML文件的位置嗎? – 2015-02-11 07:10:59

+4

歡迎來到Stack Overflow。不只是在這裏,而是在生活中的任何地方,一個好的問題描述是這樣的:「我做了* X *,我期望看到* Y *,但是我得到了* Z *。」到目前爲止,你發佈的內容根本不包括* X *,而你幾乎沒有碰到* Y *和* Z *。請[編輯]您的問題以包括必要的細節。 – 2015-02-11 08:00:42

+0

我編輯了問題並提供了必要的細節。請給我一些建議。 – 2015-02-11 09:31:04

回答

3

這裏我沒有任何問題可以重現。考慮這個方案,根據您的代碼:

{$APPTYPE CONSOLE} 

uses 
    Xml.XMLIntf, Xml.XMLDoc, Winapi.ActiveX; 

procedure Main; 
var 
    XMLDocument1, XMLDocument2: IXMLDocument; 
    LNode: IXMLNode; 
begin 
    XMLDocument1 := TXMLDocument.Create(nil); 
    XMLDocument1.Active := True; 
    XMLDocument1.Version := '1.0'; 
    XMLDocument1.Encoding := 'UTF-8'; 
    XMLDocument2 := TXMLDocument.Create(nil); 
    XMLDocument2.Active := True; 
    XMLDocument2.Version := '1.0'; 
    XMLDocument2.Encoding := 'UTF-8'; 
    LNode := XMLDocument1.CreateElement('TestElement', '101°F (38.33°C)'); 
    XMLDocument1.DocumentElement := LNode; 
    XMLDocument1.SaveToFile('c:\desktop\mk1.xml'); 
    XMLDocument2.LoadFromFile('c:\desktop\mk1.xml'); 
    Writeln(XMLDocument2.Encoding); 
end; 

begin 
    CoInitialize(nil); 
    Main; 
    Readln; 
end. 

這個程序的輸出,當XE7更新1,32位編譯,就是:

 
UTF-8 

被保存編碼與UTF文件-8。這是從記事本的截圖++當我加載文件:

enter image description here

注意度符號正確顯示,和你打算顯然已經解釋。並且還請注意窗口右下角的編碼。完全如所料,沒有BOM的UTF-8。

你有錯誤的診斷那裏實際上沒有問題。


關於您的更新,您現在似乎在詢問有關XML.Text。這是一個Delphi string,因此它總是被編碼爲UTF-16。所有的Delphi字符串都是這樣編碼的。 XML文檔編碼適用於文檔流式傳輸時。所以,當你保存到一個流或一個文件時,編碼就會發揮作用。

如果您決定要自己保存XML內容,在XML文檔對象的背後,則需要注意使用寫入編碼。所以發生在你身上的是你的代碼,我們仍然看不到,是使用錯誤的編碼。如果你將XML.Text保存爲UTF-8編碼的文件,那麼一切都會好起來的。問題是你正在用不同的編碼進行保存。很可能您使用ANSI編碼進行保存。

因此,也許你的代碼運行是這樣的:

Memo1.Lines.Text := XMLDocument1.XML.Text; 
Memo1.Lines.SaveToFile(...); 

,因爲節省了TStrings時的默認編碼這將失敗是ANSI。

+0

謝謝您的時間。但是,當我將XMLDocument2.XML.Text寫入某個XML文件並在IE中查看xml文件時出錯時,我遇到了問題。請參閱修改後的代碼。請嘗試在任何瀏覽器中打開(IE/Chrome) – 2015-02-11 13:59:45

+0

您已經完全改變了這個問題!我會更新答案。 – 2015-02-11 14:00:39

相關問題