c++qttinyxml

How to search for a node in tinyxml ? [Qt,C++]


Hi i have another question. The program needs to read an xml file, put the unit and test sections on the qtree widget with a tree hierarchy. So far i am able to write the desired sections on the qtreewidget and write the test steps when clicked on the item. But since i dont know how to make a search on the tinyxml, my code always writes the first step elements.

Here is the example of my xml file;

    <?xml version="1.0"?>
         <TestLab>
             <Units>
                 <Unit name="UTB_C" >
                     <communicationinterfaces>
                         <Interface type="UDP" ip="192.168.1.51" port="7000"/>
                    </communicationinterfaces>
                     <Tests>
                         <Test no="1" name="GPIO_3">
                             <Step no="1" name="step1" >                        
                             </Step>
                             <Step no="2"name="step2">                      
                             </Step>
                         </Test>
                     </Tests>
                </Unit>
             <Units>
                 <Unit name="UTB_C" >
                     <communicationinterfaces>
                         <Interface type="UDP" ip="192.168.1.51" port="7000"/>
                    </communicationinterfaces>
                     <Tests>
                         <Test no="1" name="GPIO_3">
                             <Step no="1" name="step1" >                        
                             </Step>
                             <Step no="2"name="step2">                      
                             </Step>
                         </Test>
                     </Tests>
                </Unit>
            </Units>
         </TestLab>

And this the code that i use to show the steps;

   while(step)
{
    QString strStep;
    strStep = step->Attribute("name");
    QLabel *label1 = new QLabel("step");
    label1->setText(strStep);
    label1->setParent(centralWidget());
    label1->move(380,y);
    label1->show();
    step = step->NextSiblingElement("Step");
    y = y+15;
}

this is the ui of the program so far.

program_ui

I want to write step of the test which the user clicks and i don't want my code to write steps when the user accidently click the parent item ( unit) but so far my program writes the first steps when user clicks any of the items. Does any one know how to search for the specific test and its step ?


Solution

  • You can use QDomElement::elementsByTagName ( const QString & tagname ) to search a specific element :

    QFile file( fileName );
    if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
    {
        qDebug( "Failed to open file for reading." );
        return ;
    }
    QDomDocument document;
    if( !document.setContent( &file ) )
    {
        qDebug( "Failed to parse the file into a DOM tree." );
        file.close();
        return ;
    }
    file.close();
    
    
    QDomElement documentElement = document.documentElement();
    
    
    QDomNodeList testElements = documentElement.elementsByTagName( "test" );
    
    for (int i=0;i<testElements.count();i++)
    {
    
         QDomElement test = testElements.item(i).toElement();
    
         if(test.attribute("no")=="1")
         {
             QDomNodeList stepElements = test.elementsByTagName( "step" );
    
             for (int j=0;j<stepElements.count();j++)
             {
    
                  QDomElement step = stepElements.item(j).toElement();
    
                  if(step.attribute("no")=="1")
                  {
                      //...
                  }
    
             }
         }
    
    }