How to skip XML file reading to end of current parent element using QXMLStreamReader?
-
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.
-
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; }
-
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?
-
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?
@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).