XML parsing



  • I am new to Qt

    I am developing application to parse the XML file. But the File format looks like this

    <Class name="ABC">
        <LayoutEntry name="ABB"            type="class:ABB"/>
        <LayoutEntry name="ACC"            type="class:ACC"/>
        <LayoutEntry name="ADD"            type="class:ADD"/>
     </Class>
    
     <Class name="ABB">   //Declared  in class ABC
        <LayoutEntry name="ABB1"            type="class:ABB1"/>
        <LayoutEntry name="ABB2"            type="class:ABB2"/>
      </Class>
    
      <Class name="ABB1">  //Declared in class ABB
        <LayoutEntry name="ABB1"            value=ABB1"/>
      </Class>
    
      <Class name="ABB2">
        <LayoutEntry name="ABB2"            value=ABB2"/>
      </Class>
    

    Please some one suggest me for any library or method in Qt to parse this type of XML . Thanks

    Edit: Added code tags -- @Wieland



  • You should give a look at the docs. If you only want to read files, QXmlStreamReader does everything you need.



  • since you can see that there is a class inside class i.e nested class , Which library will be fine for doing this



  • Hi @akshay123 ,

    Slide modification in have made for the XML provided, i have added root tag <ClassRoot>.
    Below program may help you.
    void xmlparsing::DecodeXML()
    {
    qDebug()<<Q_FUNC_INFO<<endl;
    QDomDocument DdocObj;
    QFile fobj("class.xml");
    if (!fobj.open(QIODevice::ReadOnly ))
    {
    qWarning() << "Error ::"<<fobj.errorString() << endl;
    }
    DdocObj.setContent(&fobj);
    fobj.close();
    QDomElement root =DdocObj.documentElement();

    if(root.tagName().compare("ClassRoot")==0){
        QDomNodeList CNode1 = root.childNodes();
        for(int i=0;i<CNode1.size();i++){
            QDomElement element = CNode1.item(i).toElement();
            if(element.tagName().compare("Class")==0){
                qDebug()<<"name :"<<element.attribute("name")<<endl;
                DecodeClassElement(element);
            }else{
                qDebug()<<"Tag name does not exist ::"<<element.tagName()<<endl;
            }
        }
    }else{
        qWarning() << "Wrong XML" << endl;
    }
    

    }

    void xmlparsing::DecodeClassElement(QDomElement element)
    {
    QDomNodeList CNode2 = element.childNodes();
    for(int i=0;i<CNode2.length();i++){
    QDomElement ele2 = CNode2.at(i).toElement();
    qDebug()<<"Element :"<<ele2.tagName()<<endl;
    qDebug()<<"name :"<<ele2.attribute("name")<<endl;
    qDebug()<<"type :"<<ele2.attribute("type")<<endl;
    qDebug()<<"value :"<<ele2.attribute("value")<<endl;
    }
    }


  • Moderators

    @akshay123 As @yuvaram suggested you should first fix your XML: what you posted is not valid XML. A XML document must have exactly one root node (change Root to what ever you like):

    <Root>
    <Class name="ABC">
        <LayoutEntry name="ABB"            type="class:ABB"/>
        <LayoutEntry name="ACC"            type="class:ACC"/>
        <LayoutEntry name="ADD"            type="class:ADD"/>
     </Class>
    
     <Class name="ABB">   //Declared  in class ABC
        <LayoutEntry name="ABB1"            type="class:ABB1"/>
        <LayoutEntry name="ABB2"            type="class:ABB2"/>
      </Class>
    
      <Class name="ABB1">  //Declared in class ABB
        <LayoutEntry name="ABB1"            value=ABB1"/>
      </Class>
    
      <Class name="ABB2">
        <LayoutEntry name="ABB2"            value=ABB2"/>
      </Class>
    </Root>
    


  • Hi @jsulm
    I have already mentioned that line to be added root element, please look again. I have added <ClassRoot> tag.


  • Moderators

    @yuvaram I know and I wrote that in my post. I just wanted to add the whole XML document, so it is more clear what we mean.



  • Thanks @yuvaram @jsulm @yuvaram . he XML which i have posted here is not in proper format . Since the XML is very big i wanted to post a small snippet of it . Thanks for your help .


Log in to reply
 

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