0

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.

2
  • if you search the web for example documents with "xmlns:xsi" it is easy to see they use a quote like xmlns:xsi="... and not xmlns:xsi=&quot..., and other obvious differences like a closing </whatever> tag for the root element. In your example, the terminating </m:outgoingEngineMessage> is missing. Commented Jul 13, 2011 at 8:41
  • 2
    Ouch! Don't "hand-generate" the XML - use a library to do this. It will save you a lot of trouble in the long run. Commented Jul 13, 2011 at 11:10

3 Answers 3

6

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).

Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

Yes, there are a few of us around :)
1

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

3 Comments

I still get this error message : Element type "m:outgoingEngineMessage" must be followed by either attribute specifications, "&gt;" or "/&gt;".
XML- and XHTML-documents must have white wpaces between the Attributes, the code however builds .../constraints"xmlns:m="http..., better show the correct XML code (instead of the Delphi constant declaration)
Put a space before you close the string for each line, for instance ...gw/constants" ' +

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.