3

I want to read an xml file as follow:

QFile myFile("xmlfile");  

and then continue with parsing the xml starting from :

QXmlStreamReader xmlData(myFile);

.. the error I get is:

no matching function for call to 'QXmlStreamReader::QXmlStreamReader(QFile&)'

so what is the problem and how to solve it ?


question update: Based on the selected answer below, the code is now working without a syntax error .

however, I can not read my xml. when parsing the xml, I use the following to read xml elements:

QXmlStreamReader::TokenType token = xmlElements.readNext();

then this code for checking the startElements:

 while(!xmlElements.atEnd() && !xmlElements.hasError()){ // the breakpoint is here
 do ...
 }

so, at this breakpoint, I notice in my debuger that token value is QXmlStreamReader::Invalid(1)

so, what is going on .. is my QStreamReader does not read the file as xml, or it read it but there is an error with the xml itself ?

McLan
  • 2,552
  • 9
  • 51
  • 85

3 Answers3

5

The error message is telling you that there is no constructor for the class QXmlStreamReader with the signature you are trying to invoke, i.e. the one that would accept QFile parameter alone (either by value or by reference). Reading documentation is quite helpful. Relevant extract:

QXmlStreamReader ()
QXmlStreamReader ( QIODevice * device )
QXmlStreamReader ( const QByteArray & data )
QXmlStreamReader ( const QString & data )
QXmlStreamReader ( const char * data )

Now, if you know that QFile actually inherits QIODevice (what you can find out in the documentation too), then you can immediately understand that the invocation should be changed as follows:

QXmlStreamReader xmlData(&myFile);

Furthermore, it seems like you don't know how to utilize QXmlStreamReader at all, therefore what you are looking for is a tutorial. I don't feel like rewriting great Qt tutorials here. So to intercept all your further questions, I'd refer you to the official tutorial.

Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85
  • thanks for your reply, it works without syntax error now, yet it does not read my xml. the token is stating that QXmlStreamReader::Invalid(1).. I don't know if it does not read the xml or reading it but wrong .. – McLan Apr 24 '13 at 16:32
  • Could you be more clear on what's the problem you are facing now, and update your question accordingly, and don't forget to include all the relevant output. – Alexander Shukaev Apr 24 '13 at 16:34
  • I do know exactly how QXmlStreamReader works and I already extracted a mock xml using it and it worked fine. my problem to be specific is reading a real xml file .. all i can't do is how to substitute my mock xml in the QStreamReader with the real xml file, that is all .. – McLan Apr 24 '13 at 16:55
  • Once again, we open [documentation](http://qt-project.org/doc/qt-4.8/qxmlstreamreader.html#TokenType-enum) and what do we see? We see that to learn what was the actual cause of the error we need `error()` and `errorString()`. – Alexander Shukaev Apr 24 '13 at 17:06
  • the QXmlStreamReader::errorString returned **Premature end of document** .. the good news is: the file is readable now, the bad news is: i don't know if the xml is wrong (which is something I doubt because I used my mock xml, which was working before, and the result is typical) .. or am I reading it wrongly !!!! – McLan Apr 24 '13 at 17:11
  • Obviously, I can't help you with that without seeing the actual XML and how you parse it. One thing is certain, either it is the XML which is wrong or you parse it in a wrong way. You better to figure this out yourself as nobody knows the XML and your code better than you. It shouldn't take much time just simplify the XML and try to read it without errors. Then gradually add complexity to both XML and your parsing algorithm. Software development is all about starting with simple things and then iteratively improving and adding complexity to the code. – Alexander Shukaev Apr 24 '13 at 17:15
  • 1
    PrematureEndOfDocument can always happen when reading from a file, socket etc., when the XML data is made available in chunks. One has to make sure more data is read then (connecting to readyRead signals, waitForReadyRead, etc.) and try again. To ensure it's not your file that's broken, verify it's valid XML by other means (xmllint etc.) or try with QXMLStreamReader reader(file.readAll()). That's not the nice way (as it reads everything into memory at once), but should help excluding this source of errors. – Frank Osterfeld Apr 24 '13 at 17:41
2

I found the problem and everything is now working properly

here is the mistake: by previously using the following code:

QFile myFile("xmlfile");
QXmlStreamReader xmlData(&myFile);

the file is first created in the first line, the second line of code just pass the file as it not its xml content (since it is not been opened yet), therefore the QXmlStreamReader::errorString returns Premature end of document ..

the solution is very simple: open the "myFile" file before passing it to the QXmlStreamReader using the following code:

xmlData.open(QIODevice::ReadOnly);

The device is now open for reading by QXmlStreamReader. and the final code is:

QFile myFile("xmlfile");
myFile.open(QIODevice::ReadOnly);
QXmlStreamReader xmlData(&myFile);
McLan
  • 2,552
  • 9
  • 51
  • 85
1

I read XML files a little differently in QT, I read them and store them in a seperate object, since I read and parse a lot of XMLs.

I'll post a sample for you, this sample runs off of XML attributes, if your looking for XML tags it wouldn't work.

    QDomDocument XMLfile;

    QFile XMLfile(XMLpath);
    if (!XMLfile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        // Error
    }
    if (!XMLfile.setContent(&XMLfile))
    {
        // Error
    }
    XMLfile.close();

    QDomElement root = XMLfile.firstChildElement();
    QDomElement item = root.firstChildElement();

    XMLstorage* currentXMLstorage = new XMLstorage();

    currentXMLstorage->Attribute1(item.attribute("Attribute1"));
    currentXMLstorage->Attribute2(item.attribute("Attribute2"));
    currentXMLstorage->Attribute3(item.attribute("Attribute3"));
    currentXMLstorage->Attribute4(item.attribute("Attribute4"));
    currentXMLstorage->Attribute5(item.attribute("Attribute5"));
    currentXMLstorage->Attribute6(item.attribute("Attribute6"));

    return *currentXMLstorage;
rreeves
  • 2,408
  • 6
  • 39
  • 53
  • 1
    My impression or the QDomDocument at the first line has the same identifier as the QFile in the third line, making this code uncompillable? – Momergil Jan 13 '15 at 17:19