[Qt-interest] How to: parse an xml file with tags

Yuvraj yuvraj.ragupathi at indieontech.com
Tue Feb 16 08:20:28 CET 2010


Mohammed Imran B wrote:
> Hi,
>
> I have the data something like this:-
> "<data>
>
> <ID>id1000</ID>
>
> <Name>name5</Name>
>
> <Phone>9008354576</Phone>
>
> </data>
>
> "
>
>
> I want the content present in the tags ( id1000, name5, 9008354576).
>
>
> Is there any sample example of such a kind or is there any APIs that 
> could help me in doing this.
>
>
> Thanks and regards,
>
> Imran.
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>   
Hi Imran,

hope this code will help you

QFile file(your_file_name_path);
    QDomDocument my_xml_file;
    QDomElement xml_element;
   
    if(!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::information(0,"Information","Could not able to open 
the file");
        return;
    }
    my_xml_file.setContent(&file);
    xml_element =my_xml_file.documentElement();
    while(!xml_element.isNull())
    {
        xml_parse(xml_element);
        xml_element=xml_element.nextSiblingElement();
    }



xml_parse(QDomElement &element)
{
    qDebug() << "Xm l function\n";
    QDomElement sub_element;
    sub_element =element.firstChildElement();
  while(!sub_element.isNull())
    {

      if(sub_element.tagName()=="ID")
      {
              QString data =sub_element.toElement().text();
        qDebug() << "xml data is  0: "<< data;
      }
      else if(sub_element.tagName()=="Name")
      {
                QString data =sub_element.toElement().text();
          qDebug() << "xml data is 1 : "<< data;
      }

      else if(sub_element.tagName()=="Phone")
      {
                QString data =sub_element.toElement().text();
          qDebug() << "xml data is 2 : "<< data;
      }
     
      sub_element =sub_element.nextSiblingElement();
  }

}


Thanks

Yuvraj




More information about the Qt-interest-old mailing list