Summary of the processing methods of xml files in qt

Three ways to process xml files in qt

  • XML is published by the World Wide Web Consortium (W3C) as a lightweight dialect of SHML (Standard Generalized Markup Language). The XML syntax is similar to HTML, with the main difference being that XML's tags are not fixed, but extensible. XML is designed to transmit and store data, not display (HTML).

For general processing of XML, Qt4 provides the QtXml module; for XML document Schema validation and XPath, XQuery and XSLT, Qt4 and Qt5 provide the QtXmlPatterns module.
Qt provides three processing methods

  • DOM (Document Object Model): Read the entire XML document into memory and build it into a tree structure, allowing the program to move forward and backward on the tree structure to navigate. This is the biggest difference from the other two methods, that is, it allows to achieve multiple Secondary parser (corresponds to the primary parser of QXmlStreamReader). The problem with the DOM approach is that the entire XML document needs to be read into memory at once, so it takes up a lot of memory, making it unsuitable for handling larger files. DOM is a standard interface for processing XML documents proposed by W3C.
  • SAX (Simple API for XML): Provides a large number of virtual functions to process XML documents in the form of events. This parsing method is mainly proposed for historical reasons, in order to solve the memory occupation of DOM (on modern computers, this is generally not a problem), it provides a simpler interface than DOM, and it does not require the entire XML documents are read into memory at once, so they can be used to read larger files. The SAX method is only suitable for reading XML documents.
  • QXmlStreamReader: The QXmlStreamReader class provides a fast parser for reading well-formed XML documents via a simple streaming API. It emerges as a replacement for Qt's SAX parser and is particularly suitable for implementing a parser ( The so-called "one-time parser" can be understood that we only need to read the document once, and then process the XML document from beginning to end like a traverser, without repetition, that is, the first tag will not be read. , then read the second, and then go back to the first after reading the second, which is not allowed). Using QXmlStreamReader is the fastest and easiest way to read XML in Qt. Because QXmlStreamReader uses an incremental parser, it is suitable for finding a given tag in the entire XML document, reading large files that cannot fit in memory, and processing XML custom data.

In Qt4, all three are in the QtXml module. Qt5 moved QXmlStreamReader/QXmlStreamWriter to QtCore, and QtXml was marked as "no longer maintained", which has fully demonstrated Qt's official intention.

As for generating XML documents, Qt also provides three ways:

  • Generating XML documents purely by hand is obviously the most complicated way.
  • DOM method: First generate the DOM tree in memory, and then write the DOM tree to the file. However, unless a DOM tree is already maintained in the data structure of our program, it must be troublesome to temporarily span the tree and write it again.
  • QXmlStreamWriter: Corresponds to QXmlStreamReader.
Code download: https://download.csdn.net/download/p942005405/10315742

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326127387&siteId=291194637