How to use QXmlStreamReader to parse well-formed XML, the Qt document points out that it is a faster and more convenient replacement of QXmlSimpleReader, it is also faster, in some cases, more convenient than DOM (QDomDocument. How to use QXmlStreamReader to parse well-formed XML, the Qt document points out that it is a faster and more convenient replacement of QXmlSimpleReader, it is also faster, in some cases, more convenient than DOM (QDomDocument.
XML file:
Resolution method:
Void ParseXML: parseXML (QString file_name) {if (file_name.isEmpty () return; QFile * file = new QFile (file_name); if (! File-> open (QFile: ReadOnly | QFile: Text) {QMessageBox: information (NULL, QString ("title"), QString ("open error! "); Return;} // QXmlStreamReader operates on any QIODevice. QXmlStreamReader xml (file); QList> persons; // parses XML until the end of while (! Xml. atEnd ()&&! Xml. hasError () {// read the next element. QXmlStreamReader: TokenType token = xml. readNext (); // if only StartDocument is obtained, perform the next if (token = QXmlStreamReader: StartDocument) {continue;} // if StartElement is obtained, then try to read if (token = QXmlStreamReader: StartElement) {// if it is persons, go directly to the next if (xml. name () = "persons") {continue;} // if it is person, parse it if (xml. name () = "person") {persons. append (this-> parsePerson (xml) ;}} I F (xml. hasError () {QMessageBox: information (NULL, QString ("parseXML"), xml. errorString ();} // delete all devices and data from reader and reset it to the initial status xml. clear ();} QMap ParseXML: parsePerson (QXmlStreamReader & xml) {QMap person; // check whether the person if (xml. tokenType ()! = QXmlStreamReader: StartElement & xml. name () = "person") {return person;} // get the person attribute QXmlStreamAttributes attributes = xml. attributes (); if (attributes. hasAttribute ("id") {person ["id"] = attributes. value ("id "). toString ();} // operate the next xml. readNext (); while (! (Xml. tokenType () = QXmlStreamReader: EndElement & xml. name () = "person") {if (xml. tokenType () = QXmlStreamReader: StartElement) {if (xml. name () = "name") {this-> addElementDataToMap (xml, person);} if (xml. name () = "age") {this-> addElementDataToMap (xml, person);} if (xml. name () = "email") {this-> addElementDataToMap (xml, person);} if (xml. name () = "website") {this-> addElementDataToMap (xml, pers On) ;}} xml. readNext ();} QString id = person ["id"]; QString name = person ["name"]; QString age = person ["age"]; QString email = person ["email"]; QString website = person ["website"]; return person;} void ParseXML: addElementDataToMap (QXmlStreamReader & xml, QMap & map) const {if (xml. tokenType ()! = QXmlStreamReader: StartElement) {return;} QString elementName = xml. name (). toString (); xml. readNext (); if (xml. tokenType ()! = QXmlStreamReader: Characters) {return;} map. insert (elementName, xml. text (). toString ());}
The above is the detailed content shared by the sample code for Qt parsing XML. For more information, see other related articles in the first PHP community!