Read a specific tag in xml



  • 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;
    }
    

  • Lifetime Qt Champion



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



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


  • @KroMignon said in Read a specific tag in xml:

    rootDir+xmlDir

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



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


  • Lifetime Qt Champion

    Not really, these are only paths.

    Did you meant to upload the XML file content ?



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


 

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