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 know many subnodes into a parent node in xml?
Forum Updated to NodeBB v4.3 + New Features

How to know many subnodes into a parent node in xml?

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

    Hey guys again.

    My next question is knowing about how many subnodes has a main node in an xml file using QXmlStreamReader and save it in a variable to use forward.

    example:

    QString xml = <?xml version='1.0' encoding='UTF-8'?> 
    <parent>
       <child attr="1"/>
       <child attr="2"/>
    </parent>
    

    And my code is here:

    r.addData(xml)
    if(r.readNextStartElement() && r.name() == "parent")
       //Print How many nodes has parent
       // should be 2
    }
    
    JonBJ 1 Reply Last reply
    0
    • RipleyR Ripley

      Hey guys again.

      My next question is knowing about how many subnodes has a main node in an xml file using QXmlStreamReader and save it in a variable to use forward.

      example:

      QString xml = <?xml version='1.0' encoding='UTF-8'?> 
      <parent>
         <child attr="1"/>
         <child attr="2"/>
      </parent>
      

      And my code is here:

      r.addData(xml)
      if(r.readNextStartElement() && r.name() == "parent")
         //Print How many nodes has parent
         // should be 2
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Ripley
      [I have not used it, but] QXmlStreamReader works by making calls to read the XML in a forward-only direction. The only way to "knowing about how many subnodes has a main node" is to read them, readNextStartElement()/readNext(). So you'll need to read the children from here with something like

      while (r.readNextStartElement() && r.name() == "child")
      

      and count them.

      P.S.
      Like I said I haven't used, but you'll need to do something to distinguish

      <child />
      <child />
      

      (2 child nodes) versus

      <child>
          <child />
      </child>
      

      (1 child node). The difference being that in the first case you have met the first <child>'s end element before the next start one, while in the second case you have not. Look at the return result from readNextStartElement() to see this.

      1 Reply Last reply
      1

      • Login

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