[Interest] QXmlStreamReader: Processing multiple documents from QTcpSocket

Jason H scorp1us at yahoo.com
Tue Dec 10 17:45:04 CET 2013

Well, you need to add another layer in. There are two easy ways to do that:
1. Look for the start sentinel and send everything before that to the XML reader,
2. or encapsulate them into QDataStream and read each doc as a string from the stream. 

If you are using streaming for document size issues, then 1. If you don't trust your parsing, then 2.

Either way its a just a few lines of code. 

 From: Sze Howe Koh <szehowe.koh at gmail.com>
To: "interest at qt-project.org" <interest at qt-project.org> 
Sent: Sunday, December 8, 2013 6:57 AM
Subject: [Interest] QXmlStreamReader: Processing multiple documents from	QTcpSocket


For a networked app, I created an XML-based communications protocol
over TCP (a bit like SOAP).

At first, I let QXmlStreamReader read directly from QTcpSocket. When
an EndDocument token is found, I'd call QXmlStreamReader::clear() and
setDevice(), to wait for the next message from the same socket.

This arrangement worked really well initially. However, I noticed that
if 2 messages were sent in quick succession, the end of the 1st XML
document and the start of the 2nd document would be received as a
single chunk of text. The result is an "XML declaration not at start
of document" error (or "Extra content at end of document", if the XML
declaration was omitted).

The only way I can think of around this problem is to parse the data
from the QTcpSocket, delimit it manually, then pass a single XML
document into QXmlStreamReader. This requires me to implement a
minimal XML parser myself though -- is there a nicer way?

It would be great if QXmlStreamReader can play nicely with data
streamed over a network. Would it be reasonable to ask for an option
that lets QXmlStreamReader receive multiple documents during its

Interest mailing list
Interest at qt-project.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131210/4cd5f633/attachment.html>

More information about the Interest mailing list