Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to read the files in XML



  • 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


  • Lifetime Qt Champion

    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...



  • @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();

  • Lifetime Qt Champion

    @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?



  • @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


  • Lifetime Qt Champion

    @sankarapandiyan And have you the needed access rights?



  • @aha_1980

    And have you the needed access rights?

    yes i have ,can you say how to do!


  • Lifetime Qt Champion

    @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?



  • @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



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


  • Lifetime Qt Champion

    @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?



  • @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



  • 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



  • 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.



  • @Leon_2001 ok i wil try Thanks a lot


Log in to reply