开发者

XML and Delphi problem

I am trying to implement a protocol which I will use for my application to communicate with a server. The problem is that the server is using XML so I tried to send a string to the server containing xml but I get only errors.

When I send this :

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;';

I receive an error saying:

Element type "m:outgoingEngineMessage" must be followed by either attribute specifications, "&gt ;" or "/&gt ;"

When I send this:

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;'

I get: Element not allowed in prolog...

Can some one enlighten me what I am doing wrong? I have never worked with xml files before. Is there a function to convert xml to utf8 correctly? please explain.


The safest way to generate 'well-formed' XML is using an XML library like NativeXml, OmniXML (both open source) or the MSXML library (Delphi provides a wrapper for it).


You also need to put a space at the end of each line where the line break is between attributes. You are in effect jamming them all together:

<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">'

will produce:

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

To fix this, you need to do something like the following (based on @The_Fox's code):

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>';


You are escaping < and > where you shouldn't. Only escape those entities when they are not part of the xml.

Like this:

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

And not like this:

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

So your xml would look like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜