Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to access parent node of child node in XML???
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ShrikantAmbade
    wrote on last edited by ShrikantAmbade
    #1

    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);
    
    
    
    
     }
    
    1 Reply Last reply
    0
    • ValentinMicheletV Offline
      ValentinMicheletV Offline
      ValentinMichelet
      wrote on last edited by
      #2

      Hi,

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

      S 1 Reply Last reply
      0
      • ValentinMicheletV ValentinMichelet

        Hi,

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

        S Offline
        S Offline
        ShrikantAmbade
        wrote on last edited by
        #3

        @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.

        ValentinMicheletV 1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Which error do you get?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S ShrikantAmbade

            @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.

            ValentinMicheletV Offline
            ValentinMicheletV Offline
            ValentinMichelet
            wrote on last edited by ValentinMichelet
            #5

            QDebug has not been overridden for QDomNode, use this:

            qDebug() << child.parentNode().nodeName();
            
            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved