Watch, Follow, &
Connect with Us

Welcome, Guest
Guest Settings
Help

Thread: idTCPClient + Recieve XML Data



Permlink Replies: 2 - Last Post: Mar 24, 2017 9:33 PM Last Post By: Oleg Skok Threads: [ Previous | Next ]
Oleg Skok

Posts: 2
Registered: 3/5/17
idTCPClient + Recieve XML Data
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 18, 2017 8:24 AM
Hi to ALL. First of all sorry for my English.

I try to add simple XMPP-client in my project, where I use idTCPClient already. When I receive XML package from server, I can't parse it, because it's broken.

for example (2 valid packages)
<?xml version='1.0'?>
<stream:stream xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'
id='3966489307'
from='jabber.ru'
version='1.0'
xml:lang='en'>

<stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
<compression xmlns='http://jabber.org/features/compress'>
<method>zlib</method>
</compression>
<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
<mechanism>DIGEST-MD5</mechanism>
<mechanism>PLAIN</mechanism></mechanisms>
<register xmlns='http://jabber.org/features/iq-register'/>
</stream:features>


I receive a lot of packages like
<?xml version='1.0'?>+
<stream:stream xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'
id='3966489307'
from='jabber.ru'
version='1.0'
xml:lang='en'>
<stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>

<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'

<method>zlib</method>

and etc.

Packages receiving from thread
while not Terminated do
  try
    Data := Parent.IdTCPClient.IOHandler.ReadLn(IndyTextEncoding_UTF8);
    if Length(Data) > 0 then
    begin
      Synchronize(OnExecuted);
    end;
  except
    on E: EIdException do
    begin
      Error := E.Message;
      Synchronize(OnExcepted);
    end;
  end;


Please, help fix it! How can I read XML data correctly?
Remy Lebeau (Te...


Posts: 8,641
Registered: 12/23/01
Re: idTCPClient + Recieve XML Data
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 20, 2017 11:36 AM   in response to: Oleg Skok in response to: Oleg Skok
Oleg wrote:

I try to add simple XMPP-client in my project, where I use idTCPClient
already. When I receive XML package from server, I can't parse it,
because it's broken.

The XML is not "broken", you are simply not reading it from the socket correctly
to begin with. You are assuming complete "messages" are (CR)LF delimited,
and that is not the case (read the XMPP specifications! RFC 6120 in perticular).
In XML, (CR)LF is just ignorable whitespace, not an end-of-data delimiter,
so your use of the IOHandler.ReadLn() method is just plain wrong in this
situation.

Please, help fix it!

You need to re-write your socket reading logic. There is no delimiter between
"messages", so you have to parse the XML while you are reading it from the
socket. Starting at a depth of 1, you have to recognize where one XML element
ends and the next begins so you can increment the depth counter for each
opening tag and decrement it for each closing tag, until you reach the closing
tag at depth 1. Only then will you have received a complete "message" and
can process it as needed.

This would be best handled using an XML parser that supports a pushing model
for input. You can then use the IOHandler.ReadBytes() method to read arbitrary
bytes off of the socket and "push" them into the XML engine, and let it give
you events when it has parsed completed XML elements, giving you the element
name, attributes, depth, etc.

Delphi's native XML framework does not support this kind of parsing, but
plenty of 3rd party XML engines do.

--
Remy Lebeau (TeamB)
Oleg Skok

Posts: 2
Registered: 3/5/17
Re: idTCPClient + Recieve XML Data
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 24, 2017 9:30 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy, thanks a lot for a advise! It's helpful for me!
P.S.I know that internal XML parser don't work, I use Native XML.

Edited by: Oleg Skok on Mar 24, 2017 9:32 PM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02