Sample code for parsing XML using Qt

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.