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. QDomElement, index of node ?
Forum Updated to NodeBB v4.3 + New Features

QDomElement, index of node ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 388 Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    For example if the XML has the following nodes:

    <root>
      <name><c1 /></name>
      <name><c2/></name>
       <name><c3/></name>
       <name><c4/></name>
    </root>
    

    The above is purely for the purposes of this post to illustrate what I want to do. Is there a way to get the index of any node in the DOM, in the above example, there are 4 nodes called name.

    If I pick c3, I can get the parent node 'name' from the DOM. Can I get the index of the node, so in the above I would assume that the first node called name is 0, then 1, 2 and 3 ?

    Kind Regards,
    Sy

    jsulmJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @jsulm , thank you, I've written a module which converts an XML document to JSON that was simple enough, now I want to produce a variant of this where by the JSON could contain arrays of all nodes from the XML where each node is an array of X, each element represents a single instance of the node and will have an attributes array and parent, where the parent details the node that is its parent and the index of the parent.

      This is the function that I need help completing:

      /**
       * @brief clsModXML::asArrays
       * @param robjElement : Reference to element to process
       * @param robjResult : Reference to JSON to populate
       * @param strParent : Optional, parent node name
       * @param intIndex : Optional, index of parent element
       */
      void clsModXML::asArrays(QDomElement& robjElement, QJsonObject& robjResult
                              ,QString strParent, int intIndex) {
          QDomNode::NodeType eNodeType = robjElement.nodeType();
      
          if ( eNodeType != QDomNode::ElementNode ) {
              return;
          }
          QDomNamedNodeMap mpAttr = robjElement.attributes();
          QJsonObject objAttr, objNode;
          for( int i=0; i<mpAttr.size(); i++ ) {
              QDomNode item(mpAttr.item(i));
              eNodeType = item.nodeType();
      
              if ( eNodeType != QDomNode::AttributeNode ) {
                  continue;
              }
              objAttr.insert(item.nodeName(), item.nodeValue());
          }
          objNode.insert(clsModXML::mscszAttrs, objAttr);
      
          QString strNodeName(robjElement.nodeName());
          QJsonObject::const_iterator citNode = robjResult.find(strNodeName);
          QJsonArray aryNodes;
      
          if ( citNode != robjResult.end() ) {
              aryNodes = citNode->toArray();
          }
          //Add attributes node to array
          aryNodes.append(objNode);
          robjResult.insert(strNodeName, aryNodes);
      
          if ( robjElement.hasChildNodes() == true ) {
          //Process any child nodes
              int intChildren = robjElement.childNodes().size();
              QDomNodeList lstChildNodes = robjElement.childNodes();
              for( int c=0; c<intChildren; c++ ) {
                  QDomElement objChildEl(lstChildNodes.item(c).toElement());
                  asArrays(objChildEl, robjResult, robjElement.nodeName());
              }
          }
      }
      

      The last two parameters of asArrays are optional, the nested call of asArrays has the parent node name and this is where I need to put in the index of the parent element.

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #4

      @SPlatten , fixed:

      /**
       * @brief clsModXML::asArrays
       * @param robjElement : Reference to element to process
       * @param robjResult : Reference to JSON to populate
       * @param strParent : Optional, parent node name
       * @param intIndex : Optional, index of parent element
       */
      void clsModXML::asArrays(QDomElement& robjElement, QJsonObject& robjResult
                              ,QString strParent, int intIndex) {
          QDomNode::NodeType eNodeType = robjElement.nodeType();
      
          if ( eNodeType != QDomNode::ElementNode ) {
              return;
          }
          QDomNamedNodeMap mpAttr = robjElement.attributes();
          QJsonObject objAttr, objNode;
          for( int i=0; i<mpAttr.size(); i++ ) {
              QDomNode item(mpAttr.item(i));
              eNodeType = item.nodeType();
      
              if ( eNodeType != QDomNode::AttributeNode ) {
                  continue;
              }
              objAttr.insert(item.nodeName(), item.nodeValue());
          }
          objNode.insert(clsModXML::mscszAttrs, objAttr);
      
          if ( strParent.isEmpty() != true && intIndex >= 0 ) {
              QJsonObject objParent;
              objParent.insert(strParent, intIndex);
              objNode.insert(clsModXML::mscszParent, objParent);
          }
          QString strNodeName(robjElement.nodeName());
          QJsonObject::const_iterator citNode = robjResult.find(strNodeName);
          QJsonArray aryNodes;
      
          if ( citNode != robjResult.end() ) {
              aryNodes = citNode->toArray();
          }
          //Index of parent in the array
          intIndex = aryNodes.size();
          //Add attributes node to array
          aryNodes.append(objNode);
          robjResult.insert(strNodeName, aryNodes);
      
          if ( robjElement.hasChildNodes() == true ) {
          //Process any child nodes
              int intChildren = robjElement.childNodes().size();
              QDomNodeList lstChildNodes = robjElement.childNodes();
              for( int c=0; c<intChildren; c++ ) {
                  QDomElement objChildEl(lstChildNodes.item(c).toElement());
                  asArrays(objChildEl, robjResult, robjElement.nodeName(), intIndex);
              }
          }
      }
      

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • SPlattenS SPlatten

        For example if the XML has the following nodes:

        <root>
          <name><c1 /></name>
          <name><c2/></name>
           <name><c3/></name>
           <name><c4/></name>
        </root>
        

        The above is purely for the purposes of this post to illustrate what I want to do. Is there a way to get the index of any node in the DOM, in the above example, there are 4 nodes called name.

        If I pick c3, I can get the parent node 'name' from the DOM. Can I get the index of the node, so in the above I would assume that the first node called name is 0, then 1, 2 and 3 ?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @SPlatten You can access the elements for example using https://doc.qt.io/qt-5/qdomdocument.html#elementsByTagName which returns a list and you can access the elements via an index https://doc.qt.io/qt-5/qdomnodelist.html#at. Or what is the question?

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

        SPlattenS 1 Reply Last reply
        0
        • jsulmJ jsulm

          @SPlatten You can access the elements for example using https://doc.qt.io/qt-5/qdomdocument.html#elementsByTagName which returns a list and you can access the elements via an index https://doc.qt.io/qt-5/qdomnodelist.html#at. Or what is the question?

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by SPlatten
          #3

          @jsulm , thank you, I've written a module which converts an XML document to JSON that was simple enough, now I want to produce a variant of this where by the JSON could contain arrays of all nodes from the XML where each node is an array of X, each element represents a single instance of the node and will have an attributes array and parent, where the parent details the node that is its parent and the index of the parent.

          This is the function that I need help completing:

          /**
           * @brief clsModXML::asArrays
           * @param robjElement : Reference to element to process
           * @param robjResult : Reference to JSON to populate
           * @param strParent : Optional, parent node name
           * @param intIndex : Optional, index of parent element
           */
          void clsModXML::asArrays(QDomElement& robjElement, QJsonObject& robjResult
                                  ,QString strParent, int intIndex) {
              QDomNode::NodeType eNodeType = robjElement.nodeType();
          
              if ( eNodeType != QDomNode::ElementNode ) {
                  return;
              }
              QDomNamedNodeMap mpAttr = robjElement.attributes();
              QJsonObject objAttr, objNode;
              for( int i=0; i<mpAttr.size(); i++ ) {
                  QDomNode item(mpAttr.item(i));
                  eNodeType = item.nodeType();
          
                  if ( eNodeType != QDomNode::AttributeNode ) {
                      continue;
                  }
                  objAttr.insert(item.nodeName(), item.nodeValue());
              }
              objNode.insert(clsModXML::mscszAttrs, objAttr);
          
              QString strNodeName(robjElement.nodeName());
              QJsonObject::const_iterator citNode = robjResult.find(strNodeName);
              QJsonArray aryNodes;
          
              if ( citNode != robjResult.end() ) {
                  aryNodes = citNode->toArray();
              }
              //Add attributes node to array
              aryNodes.append(objNode);
              robjResult.insert(strNodeName, aryNodes);
          
              if ( robjElement.hasChildNodes() == true ) {
              //Process any child nodes
                  int intChildren = robjElement.childNodes().size();
                  QDomNodeList lstChildNodes = robjElement.childNodes();
                  for( int c=0; c<intChildren; c++ ) {
                      QDomElement objChildEl(lstChildNodes.item(c).toElement());
                      asArrays(objChildEl, robjResult, robjElement.nodeName());
                  }
              }
          }
          

          The last two parameters of asArrays are optional, the nested call of asArrays has the parent node name and this is where I need to put in the index of the parent element.

          Kind Regards,
          Sy

          SPlattenS 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @jsulm , thank you, I've written a module which converts an XML document to JSON that was simple enough, now I want to produce a variant of this where by the JSON could contain arrays of all nodes from the XML where each node is an array of X, each element represents a single instance of the node and will have an attributes array and parent, where the parent details the node that is its parent and the index of the parent.

            This is the function that I need help completing:

            /**
             * @brief clsModXML::asArrays
             * @param robjElement : Reference to element to process
             * @param robjResult : Reference to JSON to populate
             * @param strParent : Optional, parent node name
             * @param intIndex : Optional, index of parent element
             */
            void clsModXML::asArrays(QDomElement& robjElement, QJsonObject& robjResult
                                    ,QString strParent, int intIndex) {
                QDomNode::NodeType eNodeType = robjElement.nodeType();
            
                if ( eNodeType != QDomNode::ElementNode ) {
                    return;
                }
                QDomNamedNodeMap mpAttr = robjElement.attributes();
                QJsonObject objAttr, objNode;
                for( int i=0; i<mpAttr.size(); i++ ) {
                    QDomNode item(mpAttr.item(i));
                    eNodeType = item.nodeType();
            
                    if ( eNodeType != QDomNode::AttributeNode ) {
                        continue;
                    }
                    objAttr.insert(item.nodeName(), item.nodeValue());
                }
                objNode.insert(clsModXML::mscszAttrs, objAttr);
            
                QString strNodeName(robjElement.nodeName());
                QJsonObject::const_iterator citNode = robjResult.find(strNodeName);
                QJsonArray aryNodes;
            
                if ( citNode != robjResult.end() ) {
                    aryNodes = citNode->toArray();
                }
                //Add attributes node to array
                aryNodes.append(objNode);
                robjResult.insert(strNodeName, aryNodes);
            
                if ( robjElement.hasChildNodes() == true ) {
                //Process any child nodes
                    int intChildren = robjElement.childNodes().size();
                    QDomNodeList lstChildNodes = robjElement.childNodes();
                    for( int c=0; c<intChildren; c++ ) {
                        QDomElement objChildEl(lstChildNodes.item(c).toElement());
                        asArrays(objChildEl, robjResult, robjElement.nodeName());
                    }
                }
            }
            

            The last two parameters of asArrays are optional, the nested call of asArrays has the parent node name and this is where I need to put in the index of the parent element.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #4

            @SPlatten , fixed:

            /**
             * @brief clsModXML::asArrays
             * @param robjElement : Reference to element to process
             * @param robjResult : Reference to JSON to populate
             * @param strParent : Optional, parent node name
             * @param intIndex : Optional, index of parent element
             */
            void clsModXML::asArrays(QDomElement& robjElement, QJsonObject& robjResult
                                    ,QString strParent, int intIndex) {
                QDomNode::NodeType eNodeType = robjElement.nodeType();
            
                if ( eNodeType != QDomNode::ElementNode ) {
                    return;
                }
                QDomNamedNodeMap mpAttr = robjElement.attributes();
                QJsonObject objAttr, objNode;
                for( int i=0; i<mpAttr.size(); i++ ) {
                    QDomNode item(mpAttr.item(i));
                    eNodeType = item.nodeType();
            
                    if ( eNodeType != QDomNode::AttributeNode ) {
                        continue;
                    }
                    objAttr.insert(item.nodeName(), item.nodeValue());
                }
                objNode.insert(clsModXML::mscszAttrs, objAttr);
            
                if ( strParent.isEmpty() != true && intIndex >= 0 ) {
                    QJsonObject objParent;
                    objParent.insert(strParent, intIndex);
                    objNode.insert(clsModXML::mscszParent, objParent);
                }
                QString strNodeName(robjElement.nodeName());
                QJsonObject::const_iterator citNode = robjResult.find(strNodeName);
                QJsonArray aryNodes;
            
                if ( citNode != robjResult.end() ) {
                    aryNodes = citNode->toArray();
                }
                //Index of parent in the array
                intIndex = aryNodes.size();
                //Add attributes node to array
                aryNodes.append(objNode);
                robjResult.insert(strNodeName, aryNodes);
            
                if ( robjElement.hasChildNodes() == true ) {
                //Process any child nodes
                    int intChildren = robjElement.childNodes().size();
                    QDomNodeList lstChildNodes = robjElement.childNodes();
                    for( int c=0; c<intChildren; c++ ) {
                        QDomElement objChildEl(lstChildNodes.item(c).toElement());
                        asArrays(objChildEl, robjResult, robjElement.nodeName(), intIndex);
                    }
                }
            }
            

            Kind Regards,
            Sy

            1 Reply Last reply
            0

            • Login

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