Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]XML readproblems
Forum Updated to NodeBB v4.3 + New Features

[Solved]XML readproblems

Scheduled Pinned Locked Moved General and Desktop
15 Posts 2 Posters 8.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    toho71
    wrote on last edited by
    #1

    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);
    
        i++;
    
        while (!s.atEnd())
        {
            s.readNext();
            qDebug() <<"LineNumber: " << s.lineNumber();
            if (s.isStartElement() && s.name() == "en_class")
    
            {
                k++;
                qDebug() << s.name();
                qDebug() << s.readElementText();
            }
            d++;
    
        }
    }
    qDebug() << "i = " << QString::number(i);
    qDebug() << "d = " << QString::number(d);
    qDebug() << "k = " << QString::number(k);@
    
    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

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

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • T Offline
        T Offline
        toho71
        wrote on last edited by
        #3

        Sorry here are som of the xml file

        @
        <?xml version="1.0" encoding="UTF-8" ?>
        <root>
        <version>0.2</version>
        <mains>
        <main>
        <main_id>2</main_id>

        <en_main>
        &lt;![CDATA[ Automotive]]> 
        

        </en_main>

        <en_class_title>
        <![CDATA[ Make]]>
        </en_class_title>

        • <sections>
        • <section>
          <section_id>1</section_id>
        • <en_section>
          @

        etc..
        etc..----

        [EDIT: fixed code formatting, Volker]

        1 Reply Last reply
        0
        • JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by JohanSolo
          #4

          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();
          }
          

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          1 Reply Last reply
          0
          • T Offline
            T Offline
            toho71
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • JohanSoloJ Offline
              JohanSoloJ Offline
              JohanSolo
              wrote on last edited by JohanSolo
              #6

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

              `They did not know it was impossible, so they did it.'
              -- Mark Twain

              1 Reply Last reply
              0
              • T Offline
                T Offline
                toho71
                wrote on last edited by
                #7

                Thanks
                I'll Try this tomorrow.

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

                1 Reply Last reply
                0
                • JohanSoloJ Offline
                  JohanSoloJ Offline
                  JohanSolo
                  wrote on last edited by JohanSolo
                  #8

                  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.

                  `They did not know it was impossible, so they did it.'
                  -- Mark Twain

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    toho71
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      toho71
                      wrote on last edited by
                      #10

                      This is the problem

                      @s.readElementText();@
                      anyone
                      pls

                      1 Reply Last reply
                      0
                      • JohanSoloJ Offline
                        JohanSoloJ Offline
                        JohanSolo
                        wrote on last edited by JohanSolo
                        #11

                        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?

                        `They did not know it was impossible, so they did it.'
                        -- Mark Twain

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          toho71
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • JohanSoloJ Offline
                            JohanSoloJ Offline
                            JohanSolo
                            wrote on last edited by
                            #13

                            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.

                            `They did not know it was impossible, so they did it.'
                            -- Mark Twain

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              toho71
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                toho71
                                wrote on last edited by
                                #15

                                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();
                                else
                                    qDebug() << "NO " + file.fileName();
                                
                                file.open(QIODevice::ReadOnly);
                                
                                    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());
                                
                                        xml_stream.readNext();
                                    }
                                
                                 ui->textBrowser->append("-------------------------");
                                
                                
                                 xml_stream.clear();
                                 file.close();
                                

                                }@

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved