80540

QT: QXmlStreamReader always returns “Premature End of Document” error

Question:

I have strange issue with Qt QXmlStreamReader. I'am trying to parse simple document (note: it is generated using QXmlStreamWriter):

<?xml version="1.0" encoding="UTF-8"?> <tex> <used_by/> <facade> <tags> <town_related></town_related> <zone_related></zone_related> <visual_related></visual_related> <kind_related></kind_related> <other>flamingo</other> </tags> <additional_textures> <id>flamingo_top.psd</id> </additional_textures> </facade> </tex>

Using this code:

QFile file(filename); if (file.open(QFile::ReadOnly | QFile::Text)) { QXmlStreamReader xmlReader(&file); while (xmlReader.readNextStartElement()) { /* same issue when uncommented: if (xmlReader.name() == "tex") t->readXml(xmlReader);//parse texture else*/ xmlReader.skipCurrentElement(); } if (xmlReader.hasError()) emit reportError(xmlReader.errorString()); } ...

And it always reports error "Premature end of document". Why? When debbuging, it seems, to all elements are parsed or skipped correctly.

Answer1:

I verified the behavior of your code. Indeed, it seems that readNextStartElement() does not correctly check for end of document. It only checks for start/end element to return its value, so if reading past the end of document, its internal call to readNext raises "premature end".

For a quick fix try checking for end of document yourself using readNext(), eg.:

while (!xml.atEnd()) { if (xml.readNext() != QXmlStreamReader::EndDocument) { if (xml.isStartElement()) std::cout << qPrintable(xml.name().toString()) << std::endl; } } if (xml.hasError()) std::cout << (xml.errorString().toUtf8().constData()) << std::endl;

Recommend

  • Designing iPhone application with Photoshop or any design tools
  • Android activity accessing service's static reference before the service is ready
  • How to know when stdin is empty if it contains EOF?
  • MongoError: Incorrect arguments
  • Bad request using file_get_contents for PUT request in PHP
  • $wpdb not working in file of WordPress plugin
  • Switching to Release Build causes runtime error in Web Reference
  • How to clear text inside text field when radio button is select
  • Alert pop up with LWUIT
  • How to use an array of arrays with array_map(…) in PHP?
  • Using variable in a value field in jMeter
  • Deselecting radio buttons while keeping the View Model in synch
  • Javascript simulate pressing enter in input box
  • Fetching methods from BroadcastReceiver to update UI
  • C# - Serializing and deserializing static member
  • Problems to linebreak with an int in JLabel
  • Date difference with leap year
  • How would I use PHP exceptions to define a redirect?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Join two tables and save into third-sql
  • JSON with duplicate key names losing information when parsed
  • Display Images one by one with next and previous functionality
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • jquery mobile loadPage not working
  • Matplotlib draw Spline from multiple points
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to stop GridView from loading again when I press back button?
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal