PDA

View Full Version : Why Qxmlstreamreader returns pair of quotes and double empty lines beetween elements



artt
8th December 2015, 21:52
I have such xml-file contents written with Qxmlstreamwriter:

<?xml version="1.0" encoding="UTF-8" ?>
- <FILESYSTEM>
- <FILE>
<NAME>autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
- <FILE>
<NAME>copy autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
- <FILE>
<NAME>copy copy autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
- <FILE>
<NAME>copy copy copy autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
</FILESYSTEM>

I try to extract the same view and order of this xml to console (but then in GUI TextEdit) with such
simple function:

void Mainclas::readxmlfile()
{
QXmlStreamReader Rxml;
QString filename="D:\\file00.xml";
QFile file(filename);
file.open(QIODevice::ReadOnly);
Rxml.setDevice(&file);
Rxml.readNext();
while(!Rxml.atEnd())
{
if(Rxml.isStartDocument()) {
qDebug()<<"<?xml"<<Rxml.documentEncoding ().toString()<<" "<<Rxml.documentVersion().toString()<<"?>";
Rxml.readNext();
}
if(Rxml.isStartElement()) {
qDebug()<<"<"+Rxml.name().toString()<<">";
Rxml.readNext();
}
if(Rxml.isEndElement()) {
qDebug()<<"</"+Rxml.name().toString()<<">";
Rxml.readNext();
}
if(Rxml.isCharacters ()) {
if(Rxml.text()=="") Rxml.readNext();
qDebug()<<Rxml.text();
Rxml.readNext();
}
}
}

But I got

"<FILESYSTEM" >
"
"
<FILE>
Here is console printscreen - 11566
Why it happened? how to resolve it?
Should it be some error due to absent of such eroor-handler?

anda_skoa
8th December 2015, 22:28
You are probably missing a readNext() call.
Better put it at the beginning or the end of the loop.

Cheers,
_

artt
9th December 2015, 00:02
There are several readNext() 's- where I should put next one?
Here is similar question but also without answer -
http://www.qtcentre.org/threads/64542-Why-Qxmlstreamreader-returns-pair-of-quotes-and-double-empty-lines-beetween-elements?p=285154

artt
9th December 2015, 16:44
What would you do better in this situation (to extract the xml identically), if have not any suggestion concerning quotes?

ChrisW67
9th December 2015, 19:48
The example you post is not well formed XML. I would be surprised if it was written by QXmlStreamWriter.

In all likelihood, the output at lines 2 and 3 is put there by line 25 of your code. QDebug added the quotes and between the quotes are the end of line character and leading spaces from two consecutive lines in the input file. QDebug is a debugging tool not a general purpose output generating tool. The quotes are useful to help tell the difference between an empty string "" and one containing only whitespace characters " ".

If you want a general purpose text output stream then look at QTextStream.

anda_skoa
9th December 2015, 20:07
There are several readNext() 's- where I should put next one?

The readNext() statements are all in weird places.
There should be only one at either the beginning or end that advances the stream.

Cheers,
_

artt
10th December 2015, 00:06
Really, in case of just one readnext() - in the beginning or the end of while loop we can have xml display -but the same in every detail - so my approach is correct and it doesnt influence the quotes - can I directly ask this question in trolltech team?

ChrisW67
10th December 2015, 20:43
You have already been told where the quotes came from and it has nothing to do with the QXmlStreamReader.

Trolltech has not existed since circa 2008when it became part of Nokia and later the Qt Company

artt
11th December 2015, 00:02
The example you post is not well formed XML. I would be surprised if it was written by QXmlStreamWriter.

In all likelihood, the output at lines 2 and 3 is put there by line 25 of your code. QDebug added the quotes and between the quotes are the end of line character and leading spaces from two consecutive lines in the input file. QDebug is a debugging tool not a general purpose output generating tool. The quotes are useful to help tell the difference between an empty string "" and one containing only whitespace characters " ".

If you want a general purpose text output stream then look at QTextStream.
No it seems it is well formed XML, and it is strictly craeted by QXmlStreamWriter.
Tes, the reason is in QDebug.
When I used such if {} block:


if(Rxml.isStartElement()) {
QTextStream cout(stdout,QIODevice::WriteOnly);
cout<<"<"+Rxml.name().toString()<<">";
//qDebug()<<'<'+Rxml.name().toAscii()<<'>';
//Rxml.readNext();
}

I got usual XML except NAME tag:


<?xmlUTF-8 1.0?>
<FILESYSTEM>
<FILE>
<NAME>autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
<FILE>
<NAME>copy autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
<FILE>
<NAME>copy copy autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
<FILE>
<NAME>copy copy copy autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
</FILESYSTEM>

Should it be due the fact that Name is the first sub-element(text element) of FILE?
How to do it normal without that omission as
NAME, SIZE and PATH are identical...

Added after 6 minutes:

This situation is probably to this one, if you see:

"<FILESYSTEM" >
"
"
<FILE>
"
"
<NAME> as during writing to qxmlstreamwriter I put such character as "\r\n" (that is break-line) between FILE start element and NAME start element

ChrisW67
12th December 2015, 21:46
No it seems it is well formed XML, and it is strictly craeted by QXmlStreamWriter.

From your first post:


<?xml version="1.0" encoding="UTF-8" ?>
- <FILESYSTEM>
- <FILE>
<NAME>autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
- <FILE>
<NAME>copy autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
- <FILE>
<NAME>copy copy autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
- <FILE>
<NAME>copy copy copy autoc.JPG</NAME>
<SIZE>19197</SIZE>
<PATH>c:/c</PATH>
</FILE>
</FILESYSTEM>

The hyphen '-' on line 2 is certainly not well formed XML: "Line 2 Column1 Content is not allowed in prolog". The other hyphens may or may not be correct.

Feed that to this simple reader:


#include <QCoreApplication>
#include <QFile>
#include <QXmlStreamReader>
#include <QDebug>

void readXmlFile() {
QFile input("test.xml");
if (input.open(QIODevice::ReadOnly)) {
// This block structure straight from the Qt docs
QXmlStreamReader xml(&input);
while (!xml.atEnd()) {
xml.readNext();
// ... do processing
if (xml.isStartDocument()) {
qDebug() << "Start document";
}
else if (xml.isEndDocument()) {
qDebug() << "End document";
}
else if (xml.isStartElement()) {
qDebug() << "Start element:" << xml.name();
}
else if (xml.isEndElement()) {
qDebug() << "End element:" << xml.name();
}
else if (xml.isCharacters()) {
qDebug() << "Characters:" << xml.text();
}
else {
qDebug() << "Ignored something else";
}
}
if (xml.hasError()) {
// ... do error handling
qDebug() << "Error:" << xml.error() << xml.errorString();
}
}
else {
qDebug() << "Failed to open file";
}
}

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
readXmlFile();
return 0;
}

and you get


Start document
Ignored something else
Error: 3 "Start tag expected."


I am struggling to see what you are trying to achieve. If you just want to reproduce the XML file then just copy it.