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="http://www.w3.org/2001/XMLSchema-instance">'+
'<content xsi:type="HeartBeatcmd">'+
'</content>'+
'<csq>100212</csq>'+
'</m:outgoingEngineMessage>';
I receive an error saying:
Element type "m:outgoingEngineMessage" must be followed by either attribute specifications, "> ;" or "/> ;"
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="http://www.w3.org/2001/XMLSchema-instance">'+
'<content xsi:type="开发者_运维知识库;HeartBeatcmd">'+
'</content>'+
'<csq>100212</csq>'+
'</m:outgoingEngineMessage>'
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 > 3
</content>
And not like this:
<content foo="bar%quot;>
2 + 2 > 3
</content>
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>';
精彩评论