[Solved]XML readproblems

  • I got an Xml File and I try to read it.
    But my loop is just running 1 time.

    Any suggestions
    My Code
    int i = 0;
    int d = 0;
    int k = 0;

    if (in.open(QIODevice::ReadOnly))
        QXmlStreamReader s(&in);
        while (!s.atEnd())
            qDebug() <<"LineNumber: " << s.lineNumber();
            if (s.isStartElement() && s.name() == "en_class")
                qDebug() << s.name();
                qDebug() << s.readElementText();
    qDebug() << "i = " << QString::number(i);
    qDebug() << "d = " << QString::number(d);
    qDebug() << "k = " << QString::number(k);@

  • Well, without any example of what your XML file contains it will be difficult for anybody to help you...

  • Sorry here are som of the xml file

    <?xml version="1.0" encoding="UTF-8" ?>

    &lt;![CDATA[ Automotive]]> 


    <![CDATA[ Make]]>

    • <sections>
    • <section>
    • <en_section>


    [EDIT: fixed code formatting, Volker]

  • First of all in the XML example you're giving there is no element which name is "en_class".
    Then, what if you add these lines after you loop :

    if ( s.hasError() )
      qDebug() << s.errorString();
      qDebug() << "at line " << s.lineNumber() << ", column " << s.columnNumber();
      qDebug() << "Error type " << s.error();

  • thank you
    there are elements but they are further down in the xml file

    Here is the result after your rows
    "Premature end of document."
    at line 1 , column 0
    Error type 4

    I will search this error but if you have an answer to it I will be happy

  • Look at this: it seems that your input is not complete when you are trying to parse the XML document.

  • Thanks
    I'll Try this tomorrow.

    I thought this would be enough
    @QXmlStreamReader s(&in);@

  • Well, it depends what is you in object. If it's a file on disk, I agree with you. However if you're downloading the file for instance with QNetworkAccessManager / QNetworkReply, you better wait till the file is completely downloaded and written.

    I'm pretty sure there's an example of this somewhere...

    Edit : found what I wanted, when I was trying to do such a thing this thread and this one as well helped me.

  • I got the file in the same folder as the application on the disk but I will try with another xml file

  • This is the problem


  • The readElementText() method uses readElementText( ErrorOnUnexpectedElement ).
    As far as I understand this error means that your file (assuming the reading of the full file is OK) doesn't contain valid XML: it tries to find the corresponding EndElement till the end of the file but cannot...

    Why don't you start trying to read a simple xml, like this one?

  • I did that with a simple xml file and I tried your exampel to but same error.
    This is a big problem now.

  • It means that you're using the readElementText in a wrong way... I'm afraid I cannot help you much more than I already tried. I'm not a big QXmlStreamReader user.

  • thank you for your help.
    Is it easier to use sax or dom to get the values in the xml

  • Got it to work

    @void MainWindow::init()

    //QFile file&#40;"C:/testprojekt/filesXML/cache.xml"&#41;;
    //QFile file&#40;"C:/testprojekt/filesXML/parts.xml"&#41;;
     QFile file&#40;"C:/testprojekt/filesXML/simple.xml"&#41;;
    if (file.exists())
        qDebug() << file.fileName();
        qDebug() << "NO " + file.fileName();
        QXmlStreamReader xml_stream(&file);
        while (!xml_stream.atEnd() && !xml_stream.hasError())
            if (xml_stream.isStartElement() && xml_stream.name() =="description")
                 ui->textBrowser->append( + "<" + xml_stream.name().toString()
                                         + ">" + xml_stream.readElementText());


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.