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 skip XML file reading to end of current parent element using QXMLStreamReader?
Forum Update on Monday, May 27th 2025

How to skip XML file reading to end of current parent element using QXMLStreamReader?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 2.3k 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.
  • S Offline
    S Offline
    soma_yarram
    wrote on last edited by soma_yarram
    #1

    Hello,

    Assume that I have an XML file's data as follows:

    <Parent-1>
          <Child-1>Info-1</Child-1>
          <Child-2>Info-2</Child-2>
          <Child-3>Info-3</Child-3>
    </Parent-1>
    <Parent-2>
          <Child-1>Info-1</Child-1>
          <Child-2>Info-2</Child-2>
          <Child-3>Info-3</Child-3>
    </Parent-2>
    <Parent-3>
          <Child-1>Info-1</Child-1>
          <Child-2>Info-2</Child-2>
          <Child-3>Info-3</Child-3>
    </Parent-3>
    

    When I read the file from the beginning, first I read the start element Parent-1, and then its first child Child-1 and its text Info-1.

    If the Parent-1/Child-1's Info-1 is equal to some specific value eg: SKIP, then I want to skip the entire Parent-1 element, and read next parent element Parent-2.

    As far as I know, QXMLStreamReader provides only skipCurrentElement() and readNextStartElement(), which will read until the next start element, in my case Parent-1's Child-2.

    Is there any way to skip the entire parent element? Any help is much appreciated.

    Thanks in advance.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      If your read tags in a loop, then you can just skip parsing elements until you encounter Parent-1 closing element.

      Something like this:

      QXmlStreamReader xml;
      while (!xml.atEnd()) {
          xml.readNext();
          // I assume you already read the SKIP tag
          if (xml.isEndElement() && xml.name() != QLatin1String("Parent-1"))
              continue;
      }
      

      (Z(:^

      1 Reply Last reply
      1
      • S Offline
        S Offline
        soma_yarram
        wrote on last edited by
        #3

        Thank you @sierdzio. A small question again: Is there any anyway to seek to end of file using QXMLStreamReader without reading all lines using readNext() in a loop?

        sierdzioS 1 Reply Last reply
        0
        • S soma_yarram

          Thank you @sierdzio. A small question again: Is there any anyway to seek to end of file using QXMLStreamReader without reading all lines using readNext() in a loop?

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @soma_yarram said in How to skip XML file reading to end of current parent element using QXMLStreamReader?:

          Thank you @sierdzio. A small question again: Is there any anyway to seek to end of file using QXMLStreamReader without reading all lines using readNext() in a loop?

          No. It is a stream reader, it works on a (continuous) stream of data. The only way in which QXmlStreamReader can progress further is to read next element; it can't jump. At least that is how I remember it.

          You can seach for closing tag (if you know it's name) in a different way, though - read the file into a QByteArray or QString, then perform search from the back for your tag (using indexOf() method for example).

          (Z(:^

          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