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. How to read the files in XML
Forum Updated to NodeBB v4.3 + New Features

How to read the files in XML

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 1.9k Views
  • 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.
  • sankarapandiyanS Offline
    sankarapandiyanS Offline
    sankarapandiyan
    wrote on last edited by sankarapandiyan
    #1

    i am tried to merge the xml file, and the files are not open and read

    i have the doubt in the file declaration please kindly resolve this guys ,
    And i have the all xml files in the xmlfileouput (this is the file name of all xml files)

    This is my code

    void readFile(QString filename, QTextStream &writeTo )
            {
        QFile file("/home/newuser/Desktop/xmlfileoutput/filenamemw.xml/setting1xmlpage2.xml/setting2xmlpage3.xml/setting3xmlpage4.xml");
         // QFile file("/home/newuser/Desktop/xmlfileoutput");
        //    QFile file(xmlfileoutput);
    
    if(!file.open(QFile::ReadOnly |
                  QFile::Text))
    {
        qDebug() << " Could not open the file for reading";
        return;
    }
    
    QTextStream in(&file);
    writeTo << in.readAll(); // read all and write to other file
    file.close();
       }
    
      void saveAlltoOne() {
     QFile file("allinall.xml");
    // Trying to open in WriteOnly and Text mode
    if(!file.open(QFile::WriteOnly |
                  QFile::Text))
    {
        qDebug() << " Could not open file for writing";
        return;
    }
    
    QTextStream out(&file);
    
    readFile("filenamemw.xml", out);
    readFile("setting1xmlpage2.xml", out);
    readFile("setting2xmlpage3.xml", out);
    readFile("setting3xmlpage4.xml", out);
    
    file.flush();
    file.close();
      void MainWindow::on_okbutn_clicked()
     {
         saveAlltoOne();
    }
    

    Thanks in advance

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You pass a parameter named 'filename' but then also create a local variable 'filename'. And you even don't use the passed parameter at all...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • sankarapandiyanS Offline
        sankarapandiyanS Offline
        sankarapandiyan
        wrote on last edited by
        #3

        @Christian-Ehrlicher ok sure i have changed the code now i got this error , i am not able to read
        this is my code and i have this error

        error:Could not open the file for reading
        Could not open the file for reading
        Could not open the file for reading
        Could not open the file for reading

                void readFile(QString filename, QTextStream &writeTo )
        {
        QFile file("/home/newuser/Desktop/xmlfileoutput/filenamemw.xml/setting1xmlpage2.xml/setting2xmlpage3.xml/setting3xmlpage4.xml");
        
        if(!file.open(QFile::ReadOnly |
                      QFile::Text))
        {
            qDebug() << " Could not open the file for reading";
            return;
        }
        else{
            qDebug() << "opened";}
        
            QTextStream in(&file);
        writeTo << in.readAll(); // read all and write to other file
        file.close();
        
        aha_1980A 1 Reply Last reply
        0
        • sankarapandiyanS sankarapandiyan

          @Christian-Ehrlicher ok sure i have changed the code now i got this error , i am not able to read
          this is my code and i have this error

          error:Could not open the file for reading
          Could not open the file for reading
          Could not open the file for reading
          Could not open the file for reading

                  void readFile(QString filename, QTextStream &writeTo )
          {
          QFile file("/home/newuser/Desktop/xmlfileoutput/filenamemw.xml/setting1xmlpage2.xml/setting2xmlpage3.xml/setting3xmlpage4.xml");
          
          if(!file.open(QFile::ReadOnly |
                        QFile::Text))
          {
              qDebug() << " Could not open the file for reading";
              return;
          }
          else{
              qDebug() << "opened";}
          
              QTextStream in(&file);
          writeTo << in.readAll(); // read all and write to other file
          file.close();
          
          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @sankarapandiyan said in I Got this as a error: declaration of 'QString filename' shadows a parameter:

          And do you already have such a file

          /home/newuser/Desktop/xmlfileoutput/filenamemw.xml/setting1xmlpage2.xml/setting2xmlpage3.xml/setting3xmlpage4.xml

          if you want to open it for reading?

          Qt has to stay free or it will die.

          1 Reply Last reply
          0
          • sankarapandiyanS Offline
            sankarapandiyanS Offline
            sankarapandiyan
            wrote on last edited by sankarapandiyan
            #5

            @aha_1980 said in I Got this as a error: declaration of 'QString filename' shadows a parameter:

            if you want to open it for reading?

            yes how to open and read and write

            aha_1980A 1 Reply Last reply
            0
            • sankarapandiyanS sankarapandiyan

              @aha_1980 said in I Got this as a error: declaration of 'QString filename' shadows a parameter:

              if you want to open it for reading?

              yes how to open and read and write

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @sankarapandiyan And have you the needed access rights?

              Qt has to stay free or it will die.

              sankarapandiyanS 1 Reply Last reply
              0
              • aha_1980A aha_1980

                @sankarapandiyan And have you the needed access rights?

                sankarapandiyanS Offline
                sankarapandiyanS Offline
                sankarapandiyan
                wrote on last edited by sankarapandiyan
                #7

                @aha_1980

                And have you the needed access rights?

                yes i have ,can you say how to do!

                aha_1980A 1 Reply Last reply
                0
                • sankarapandiyanS sankarapandiyan

                  @aha_1980

                  And have you the needed access rights?

                  yes i have ,can you say how to do!

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @sankarapandiyan Then your code should work!

                  What is the output of:

                  file /home/newuser/Desktop/xmlfileoutput/filenamemw.xml/setting1xmlpage2.xml/setting2xmlpage3.xml/setting3xmlpage4.xml

                  in a terminal?

                  Qt has to stay free or it will die.

                  sankarapandiyanS 2 Replies Last reply
                  0
                  • aha_1980A aha_1980

                    @sankarapandiyan Then your code should work!

                    What is the output of:

                    file /home/newuser/Desktop/xmlfileoutput/filenamemw.xml/setting1xmlpage2.xml/setting2xmlpage3.xml/setting3xmlpage4.xml

                    in a terminal?

                    sankarapandiyanS Offline
                    sankarapandiyanS Offline
                    sankarapandiyan
                    wrote on last edited by
                    #9

                    @aha_1980
                    i am not able to read
                    error:Could not open the file for reading
                    Could not open the file for reading
                    Could not open the file for reading
                    Could not open the file for reading

                    1 Reply Last reply
                    0
                    • aha_1980A aha_1980

                      @sankarapandiyan Then your code should work!

                      What is the output of:

                      file /home/newuser/Desktop/xmlfileoutput/filenamemw.xml/setting1xmlpage2.xml/setting2xmlpage3.xml/setting3xmlpage4.xml

                      in a terminal?

                      sankarapandiyanS Offline
                      sankarapandiyanS Offline
                      sankarapandiyan
                      wrote on last edited by
                      #10

                      @aha_1980
                      My problem is that i put four file names into one path
                      what to do ?
                      i think this is the problem

                      aha_1980A 1 Reply Last reply
                      0
                      • sankarapandiyanS sankarapandiyan

                        @aha_1980
                        My problem is that i put four file names into one path
                        what to do ?
                        i think this is the problem

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @sankarapandiyan

                        You should first open only /home/newuser/Desktop/xmlfileoutput/filenamemw.xml and read it.

                        After that finished, you can read the second file and so on.

                        Ok?

                        Qt has to stay free or it will die.

                        sankarapandiyanS 1 Reply Last reply
                        1
                        • aha_1980A aha_1980

                          @sankarapandiyan

                          You should first open only /home/newuser/Desktop/xmlfileoutput/filenamemw.xml and read it.

                          After that finished, you can read the second file and so on.

                          Ok?

                          sankarapandiyanS Offline
                          sankarapandiyanS Offline
                          sankarapandiyan
                          wrote on last edited by sankarapandiyan
                          #12

                          @aha_1980
                          QFile file("/home/newuser/Desktop/xmlfileoutput/filenamemw.xml");
                          yes now it opens , But unable to read it
                          this the output,
                          Starting /home/newuser/Desktop/designtest-build-desktop-Qt_4_8_1_in_PATH__System__Release/designtest...
                          opened
                          opened
                          opened
                          opened

                          QFile file("/home/newuser/Desktop/xmlfileoutput/filenamemw.xml/setting1xmlpage2.xml");

                          if i add the second file and run the code ,the output is like this ,
                          Starting /home/newuser/Desktop/designtest-build-desktop-Qt_4_8_1_in_PATH__System__Release/designtest...
                          Could not open the file for reading
                          Could not open the file for reading
                          Could not open the file for reading
                          Could not open the file for reading

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            Leon_2001
                            wrote on last edited by
                            #13

                            This is not how QFile works...

                            QFile expects from you to give a path to the constructor.
                            Therefore if you have 4 files you need 4 QFiles (Or 1 file and change the path with the "setFileName" method of QFile after finished one reading).

                            QFile file1("/home/newuser/Desktop/xmlfileoutput/filenamemw.xml");
                            QFile file2("/home/newuser/Desktop/xmlfileoutput/setting1xmlpage2.xml");
                            (not sure if this is the correct path as I can't look into your file Manager)
                            ...
                            or simply after you have finished reading file1
                            file1.setFileName("/home/newuser/Desktop/xmlfileoutput/setting1xmlpage2.xml");

                            I suppose, you function header should look therefore more like this:

                            void copyFiles(const QStringList& sources, const QString& destination);

                            You first open your file with destionation as the path for writing, as this is the file where you copy the 4 other xml files in.

                            Next you loop over the QStringLists with a for each loop. In it you open a file with the path you get form QStringList. Read it line for line and write the content line for line in the destination file.

                            You call could look like this

                            int main() {
                              QStringList pathes = {"/home/newuser/Desktop/xmlfileoutput/filenamemw.xml", "/home/newuser/Desktop/xmlfileoutput/setting1xmlpage2.xml"};
                             
                              copyFiles(pathes, "/path/to/your/destination/file");
                            }
                            

                            If you get something like "could not open" first test if you give the right path to the file by using the command aha_1980 recommended you:
                            $ file /path/to/your/file

                            1 Reply Last reply
                            4
                            • L Offline
                              L Offline
                              Leon_2001
                              wrote on last edited by
                              #14

                              By the way, depending on your file manager, you usually can click right on the file and there will be something in the menu like "properties" if you click on it, you will see the path to the file or to the folder the file is in it.
                              In last case you need to add /yourFileName to get the complete path.

                              1 Reply Last reply
                              3
                              • sankarapandiyanS Offline
                                sankarapandiyanS Offline
                                sankarapandiyan
                                wrote on last edited by sankarapandiyan
                                #15

                                @Leon_2001 ok i wil try Thanks a lot

                                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