How to access parent node of child node in XML???



  • I am trying to read XML file and want to know the parent of child node.
    Also can I get the parent of QDomElement?? Like I define QDomElement in my code that is child in ListElements function???
    my code is as follow:

    #include<QWidget>
    #include "importxml.h"
    
    
    ImportXML::ImportXML(QString filename)
    
    {
        QString inputfile = filename;
        qDebug()<<inputfile;
    
    }
    void ImportXML::SubNodeList(QString ChildName, QDomElement child){
      if(ChildName =="state-space"){
          ListElements(child);
      }
      else if(ChildName =="extension-vector"){
          ListElements(child);
      }
      else if(ChildName =="input-vector"){
           ListElements(child);
      }
      else if(ChildName =="authorisation-scheme"){
           ListElements(child);
      }
    else if (ChildName=="set-component"){
          ListElements(child);
      }
      else if (ChildName=="relation-component"){
            ListElements(child);
        }
      else if (ChildName=="mapping-component"){
            ListElements(child);
        }
      else if (ChildName=="name"){
            ListElements(child);
            qDebug()<< child.text();
        }
      else if (ChildName=="values"){
            ListElements(child);
        }
      else if (ChildName=="elements"){
            ListElements(child);
        }
      else if (ChildName=="mapping-type"){
            ListElements(child);
        }
      else if (ChildName=="key-components"){
            ListElements(child);
        }
      else if (ChildName=="value-component"){
            ListElements(child);
        }
      else if (ChildName=="set-elem"){
            ListElements(child);
            qDebug()<<child.text();
        }
      else if (ChildName=="set"){
            ListElements(child);
            qDebug()<<child.text();
        }
      else if (ChildName=="tuple"){
            ListElements(child);
        }
      else if (ChildName=="value"){
            ListElements(child);
            qDebug()<<child.text();
               }
      else if (ChildName=="keys"){
            ListElements(child);
        }
    
    }
    
    void ImportXML::ListElements(QDomElement root)
    {
        QDomElement child;
       QDomNodeList childelement = root.childNodes();
           for(int i=0; i<childelement.count(); i++){
            QDomNode childnode = childelement.at(i);
    
            //convert to element
            if(childnode.isElement())
            {
                child = childnode.toElement();
               qDebug() << "Direct children of root node are " <<child.nodeName();
            }
            QString childname=child.nodeName();
            SubNodeList(childname,child);
        }
    }
    
    /* Read and load the xml file from the disk */
    
    void ImportXML::readfile(QString filename){
        qDebug()<<filename;
    
      QDomDocument document;
    
            // Load the XML file or open the XML file
    
            QFile file(filename);
            if(!file.open(QIODevice::ReadOnly |QIODevice::Text)){
                qDebug() << "Failed to open the file";
                return;
            }
            else{
                if(!document.setContent(&file)){
                    qDebug()<<"Failed to get the content";
                    return;
                }
                file.close();
            }
    /*readig root element*/
    
           QDomElement root = document.firstChildElement();
           qDebug()<<root.nodeName();
    
     /*Reading direct child of the root element*/
           ListElements(root);
    
    
    
    
     }
    


  • Hi,

    What about parentNode() method?
    http://doc.qt.io/qt-5/qdomnode.html#parentNode



  • @ValentinMichelet Thank you so much for reply.
    I checked parentNode() method, but when I write
    qDebug()<< child.parentNode();
    then it gives me error.. I dont know what should be the correct syntax for it.


  • Moderators

    Which error do you get?



  • QDebug has not been overridden for QDomNode, use this:

    qDebug() << child.parentNode().nodeName();
    

Log in to reply
 

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