PDA

View Full Version : Best way to handle XML fragments?



pherthyl
18th January 2010, 21:03
I'm currently implementing a TCP API that works by sending commands as XML fragments over TCP (spec here: http://mirametrix.com/images/stories/files/Open_Eye_Gaze_API_V1.0.pdf )

When parsing commands, I could just process the strings myself, but that seems to defeat the purpose of XML. I'd rather take advantage of Qt's XML processing capabilities. Initially I was thinking I would create a QDomElement for each received command and then use its functions to get the command type and attributes. However I can't find a way to create a QDomElement from a string.

Is there an easy way to use Qt's XML classes when the data is just XML fragments instead of a complete document? QXmlStreamReader seems promising but I think it wants full documents, and it might be overkill for the purposes of reading single elements...

aamer4yu
19th January 2010, 07:21
QDomDocument::setContent
Just use this function to see if you have got valid XML.
Something like -

if(doc.setContent(buffer))
{
// you got valid xml in doc
buffer = buffer.remove(doc.string()); // remove the valid xml from buffer
}
else
// continue or wait for next data

pherthyl
19th January 2010, 19:21
I ended up using QXmlStreamReader/Writer. I thought I would need complete XML docs, but it works just fine on fragments, as long as those fragments are valid XML.