xmlqtqxmlstreamreader

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


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.


Solution

  • If calling skipCurrentElement() twice does not work, you should easily be able to skip with a simple loop

    while (!isEndElement() || name() != "Parent-1") {
        readNext();
    }