Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Read a specific tag in xml
QtWS25 Last Chance

Read a specific tag in xml

Scheduled Pinned Locked Moved Solved Language Bindings
8 Posts 3 Posters 2.0k 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.
  • N Offline
    N Offline
    Nathan Miguel
    wrote on last edited by
    #1

    In summary, I created a program in python for it to create an xml with system information, but I can not seem to make it read a specific tag of the file. My attempts were using QDom and QXmlStreamReader

    XMLFILE

    <root><cpuname name="namecpu">Intel(R) Pentium(R) CPU G4400 @ 3.30GHz</cpuname><cpuvendor name="cpucorp">GenuineIntel</cpuvendor><gpuname name="gputag">GeForce GTX 750 Ti</gpuname><gpuvendor name="gpucorp">NVIDIA Corporation</gpuvendor></root>
    

    FUNCTION CPP

    QString systeminfo::getCpuName()
    {
        QString rootDir = QDir::currentPath();
        QString xmlDir = "/sysinfo/system.xml";
        QDomDocument docxml;
        QString test;
        QFile xmlread;
        xmlread.setFileName(rootDir+xmlDir);
    
        test = "head";
    
        QXmlStreamReader reader(&xmlread);
        if (xmlread.open(QIODevice::ReadOnly))
        {
            if (docxml.setContent(&xmlread)){ //Open xml with success
                //Read specifc tag xml
                test = "xml loaded with success"; //the program returned this string successfully now only missing I need to explain to me how I can read xml
            }
    
        }
    
        return test;
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you mean QDomDocument::elementsByTagName ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Do you mean QDomDocument::elementsByTagName ?

        N Offline
        N Offline
        Nathan Miguel
        wrote on last edited by
        #3

        @SGaist I already tried to use this face but I did not understand how it works

        KroMignonK 1 Reply Last reply
        0
        • N Nathan Miguel

          @SGaist I already tried to use this face but I did not understand how it works

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @Nathan-Miguel Have you tried this:

          QString systeminfo::getCpuName()
          {
              QString rootDir = QDir::currentPath();
              QString xmlDir = "/sysinfo/system.xml";
              QDomDocument docxml;
              QString test;
              QFile xmlread;
              xmlread.setFileName(rootDir+xmlDir);
          
              test = "head";
          
              QXmlStreamReader reader(&xmlread);
              if (xmlread.open(QIODevice::ReadOnly))
              {
                  if (docxml.setContent(&xmlread)){ //Open xml with success
                      test = "xml loaded with success"; 
                      //Read specifc tag xml
                      auto nodes = dom.elementsByTagName("cpuname");
                      for(int i = 0; i < nodes.count(); i++)
                      {
                          QDomNode elm = nodes.at(i);
                          if(elm.isElement())
                          {
                              test = QStringLiteral("%1 = %2").arg(elm.toElement().tagName(), elm.toElement().text());
                          }
                      }
                  }
              }
          
              return test;
          }
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          N 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @Nathan-Miguel Have you tried this:

            QString systeminfo::getCpuName()
            {
                QString rootDir = QDir::currentPath();
                QString xmlDir = "/sysinfo/system.xml";
                QDomDocument docxml;
                QString test;
                QFile xmlread;
                xmlread.setFileName(rootDir+xmlDir);
            
                test = "head";
            
                QXmlStreamReader reader(&xmlread);
                if (xmlread.open(QIODevice::ReadOnly))
                {
                    if (docxml.setContent(&xmlread)){ //Open xml with success
                        test = "xml loaded with success"; 
                        //Read specifc tag xml
                        auto nodes = dom.elementsByTagName("cpuname");
                        for(int i = 0; i < nodes.count(); i++)
                        {
                            QDomNode elm = nodes.at(i);
                            if(elm.isElement())
                            {
                                test = QStringLiteral("%1 = %2").arg(elm.toElement().tagName(), elm.toElement().text());
                            }
                        }
                    }
                }
            
                return test;
            }
            
            N Offline
            N Offline
            Nathan Miguel
            wrote on last edited by
            #5

            @KroMignon said in Read a specific tag in xml:

            rootDir+xmlDir

            Yes I had tried this in the forum pile that I looked for

            N 1 Reply Last reply
            0
            • N Nathan Miguel

              @KroMignon said in Read a specific tag in xml:

              rootDir+xmlDir

              Yes I had tried this in the forum pile that I looked for

              N Offline
              N Offline
              Nathan Miguel
              wrote on last edited by
              #6

              @Nathan-Miguel if this helps solve the problem

              PROGRAM DIRECTORY: E:\Git\Denoiser-Script\QML_C++\build-Denosier-Desktop_Qt_5_12_3_MSVC2017_64bit2-Debug\debug

              XML DIRECTORY: E:\Git\Denoiser-Script\QML_C++\build-Denosier-Desktop_Qt_5_12_3_MSVC2017_64bit2-Debug\debug\sysinfo\system.xml

              KroMignonK 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Not really, these are only paths.

                Did you meant to upload the XML file content ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • N Nathan Miguel

                  @Nathan-Miguel if this helps solve the problem

                  PROGRAM DIRECTORY: E:\Git\Denoiser-Script\QML_C++\build-Denosier-Desktop_Qt_5_12_3_MSVC2017_64bit2-Debug\debug

                  XML DIRECTORY: E:\Git\Denoiser-Script\QML_C++\build-Denosier-Desktop_Qt_5_12_3_MSVC2017_64bit2-Debug\debug\sysinfo\system.xml

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by KroMignon
                  #8

                  @Nathan-Miguel said in Read a specific tag in xml:

                  PROGRAM DIRECTORY: E:\Git\Denoiser-Script\QML_C++\build-Denosier-Desktop_Qt_5_12_3_MSVC2017_64bit2-Debug\debug
                  XML DIRECTORY: E:\Git\Denoiser-Script\QML_C++\build-Denosier-Desktop_Qt_5_12_3_MSVC2017_64bit2-Debug\debug\sysinfo\system.xml

                  In your code extract you have hard coded the path and with you output it should be:

                  QString systeminfo::getCpuName()
                  {
                      QString rootDir = qApp->applicationDirPath(); // QDir::currentPath() != not application path
                      QString xmlDir = "/sysinfo/system.xml";
                      QDomDocument docxml;
                      QString test;
                      QFile xmlread;
                      xmlread.setFileName(rootDir+xmlDir);
                  
                      test = "head";
                  
                      if (xmlread.open(QIODevice::ReadOnly))
                      {
                          QXmlStreamReader reader(&xmlread);
                          if (docxml.setContent(&xmlread)){ //Open xml with success
                              test = "xml loaded with success";
                              //Read specifc tag xml
                              auto nodes = docxml.elementsByTagName("cpuname");
                              for(int i = 0; i < nodes.count(); i++)
                              {
                                  QDomNode elm = nodes.at(i);
                                  if(elm.isElement())
                                  {
                                      test = QStringLiteral("%1 = %2").arg(elm.toElement().tagName(), elm.toElement().text());
                                  }
                              }
                          }
                      }
                  
                      return test;
                  }
                  

                  ==> my output with this code and your XML document is:
                  0_1561445518059_240c2702-943f-4bcc-b78f-e2a3dabc860e-image.png

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  1

                  • Login

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