0

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 ?

Ashtaroth
  • 47
  • 10

2 Answers2

0

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")
              {
                  //...
              }

         }
     }

}
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Thanks for the answer that is great and looks realy neat but is there anyway to do this with tinyxml ? I am not using QtXml and the project requires using tinyxml. – Ashtaroth Aug 07 '14 at 13:21
  • There is a function which is almost the same as the one Nejat pointed at: `TiXmlHandle::FirstChildElement(const char *value) const` (Return a handle to the first child element with the given name.). See http://www.grinninglizard.com/tinyxmldocs/index.html for more information ;) – Martin Aug 07 '14 at 13:36
-2

http://www.cnblogs.com/lyggqm/p/4565749.html

http://blog.csdn.net/lujianxin1098/article/details/

const char * cXmlName = "files.xml";
string strPath;
// createXml(ccXmlName);
// readXml(cXmlName);
// insert(ccXmlName);
// readXml(ccXmlName);

ShowSingleNode(cXmlName, "Document", "title",
               "About VMware Player Help");

strPath = PathOpt();

bool ret = ModifySingleNode(cXmlName, "Document", "title",
                            "About VMware Player Help", strPath);

if (ret)
{
    cout << "OK" << endl;
}
else
{
    cout << "false" << endl;
}

ShowSingleNode("hhhhhhhhhhhhh.xml", "Document",
               "title", "About VMware Player Help");
return 0;
JB.
  • 40,344
  • 12
  • 79
  • 106
ffd
  • 1