2011-07-13 55 views
0

我想實現一個協議,我將用它來與應用程序進行通信。問題是服務器使用XML,所以我試圖發送一個字符串到包含xml的服務器,但我只得到錯誤。XML和Delphi問題

當我發送此:

mymsg: String = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+ 
'<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints"'+ 
'xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg"'+ 
'xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;'+ 
'&lt;content xsi:type=&quot;HeartBeatcmd&quot;&gt;'+ 
'&lt;/content&gt;'+ 
'&lt;csq&gt;100212&lt;/csq&gt;'+ 
'&lt;/m:outgoingEngineMessage&gt;'; 

我收到一條錯誤消息:

元素類型 「M:outgoingEngineMessage」 必須跟任 屬性規格, 「& gt;」 中或「/ & gt」;

當我發送此:

mymsg : String = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+ 
    '&lt;m:outgoingEngineMessage xmlns:c=&quot;http://www.bvb.ro/xml/ns/arena/gw/constraints"'+ 
    'xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg"'+ 
    'xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;'+ 
    '&lt;content xsi:type=&quot;HeartBeatcmd&quot;&gt;'+ 
    '&lt;/content&gt;'+ 
    '&lt;csq&gt;100212&lt;/csq&gt;'+ 
    '&lt;/m:outgoingEngineMessage&gt;' 

我得到:元素不允許在序言...

有人可以告訴我什麼,我做錯了什麼?我以前從未使用過xml文件。有沒有一個函數可以正確地將xml轉換爲utf8?請解釋。

+0

如果你在網上搜索例如具有「xmlns:xsi」的文檔,很容易看到他們使用像'xmlns:xsi =「...'而不是'xmlns:xsi =&quot;'這樣的引號,並且其他明顯的差異,如關閉''標記佛r根元素。在你的例子中,終止的缺失。 – mjn

+2

哎唷!不要「手動生成」XML - 使用庫來執行此操作。從長遠來看,這將爲您節省很多麻煩。 – Misha

回答

4

您還需要在把一個空間換行符在屬性之間的每一行的結尾。你實際上是干擾它們放在一起:

<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints"'+ 
'xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg"'+ 
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' 

會產生:

<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints"xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

要解決這個問題,你需要做類似如下(根據@ The_Fox的代碼):

mymsg: String = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+ 
'<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints" '+ 
//               see the space here --^ 
'xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg" '+ 
//         and here --^ 
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+ 
'<content xsi:type="HeartBeatcmd">'+ 
'</content>'+ 
'<csq>100212</csq>'+ 
'</m:outgoingEngineMessage>'; 
+0

作品謝謝!不知道女編碼員是否存在 – opc0de

+2

是的,我們身邊有幾個人:) – Nat

6

生成「格式良好」XML的最安全方法是使用像NativeXml,OmniXML(均爲開源)或MSXML庫(Delphi爲其提供封裝)的XML庫。

1

你正在逃避<和>你不應該在哪裏。當它們不是xml的一部分時,只能轉義這些實體。

像這樣:

<content foo="bar"> 
2 + 2 &gt; 3 
</content> 

,而不是像這樣:

&lt;content foo=&quot;bar%quot;&gt; 
2 + 2 &gt; 3 
&lt;/content&gt; 

所以你的XML應該是這樣的:

mymsg: String = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+ 
'<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints" '+ 
'xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg" '+ 
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+ 
'<content xsi:type="HeartBeatcmd">'+ 
'</content>'+ 
'<csq>100212</csq>'+ 
'</m:outgoingEngineMessage>'; 
+0

我仍然收到以下錯誤消息:元素類型「m:outgoingEngineMessage」必須後跟任一屬性規範,「>」或「/ >」。 – opc0de

+0

XML和XHTML文檔必須在屬性之間有白色空白,然而代碼會建立'.../constraints「xmlns:m =」http ...',最好顯示正確的XML代碼(而不是Delphi常量聲明) – mjn

+0

在關閉每行的字符串之前放置一個空格,例如'... gw/constants''+' – Nat