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 Updated to NodeBB v4.3 + New Features

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.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
    soma_yarram
    wrote on 4 Nov 2016, 10:11 last edited by soma_yarram 11 Apr 2016, 17:23
    #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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 5 Nov 2016, 16:25 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 8 Nov 2016, 09:10 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?

        S 1 Reply Last reply 8 Nov 2016, 09:27
        0
        • S soma_yarram
          8 Nov 2016, 09:10

          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?

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 8 Nov 2016, 09:27 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

          4/4

          8 Nov 2016, 09:27

          • Login

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