C ++ parses XML using TinyXML

Source: Internet
Author: User

Occasionally, I saw this problem at the hero meeting. I haven't compiled it for a long time. The following is the address: http://hero.pongo.cn /. As a result, I began to search on the Internet, most of which were implemented by using the added class libraries. So I tried the online statement first, no matter how it was implemented, and then I did it first. Download TinyXML and Baidu. Download and decompress the file. There are two header files and four cpp files, and add them to the project together. xmlTest. cpp is a file containing the main function. The code below is very simple. In any case, it is finally implemented. Although the question requires not to be implemented using an additional class library, after a period of time, I will solve the problem and then update the implementation method without external libraries.

<School name = "Software Institute"> <Class name = "C ++"> <Student name = "tinyxml" number = "123"> <email> tinyxml@163.com </email> <address> China </address> </Student> <Student name = "jsoncpp" number = "456"> <email> jsoncpp@gmail.com </email> <address> USA </address> </Student> </Class> </School>

 

The above is to read the xml file, and the following is xmlTest. cpp.
# Include <iostream> # include "tinystr. h "# include" tinyxml. h "# include <string> using namespace std; void readSchoolXml () {using namespace std; const char * xmlFile =" school. xml "; TiXmlDocument doc; if (doc. loadFile (xmlFile) {doc. print ();} else {cout <"can not parse school. xml "<endl; return;} TiXmlElement * rootElement = doc. rootElement (); // School element TiXmlElement * classElement = rootElem Ent-> FirstChildElement (); // Class Element TiXmlElement * studentElement = classElement-> FirstChildElement (); // Students for (; studentElement! = NULL; studentElement = studentElement-> NextSiblingElement () {TiXmlAttribute * attributeOfStudent = studentElement-> FirstAttribute (); // obtain the name attribute of student for (; attributeOfStudent! = NULL; attributeOfStudent = attributeOfStudent-> Next () {cout <attributeOfStudent-> Name () <":" <attributeOfStudent-> Value () <std :: endl;} TiXmlElement * studentContactElement = studentElement-> FirstChildElement (); // obtain the first contact information of student for (; studentContactElement! = NULL; studentContactElement = studentContactElement-> NextSiblingElement () {string contactType = studentContactElement-> Value (); string contactValue = studentContactElement-> GetText (); cout <contactType <":" <contactValue <std: endl ;}} int main () {readSchoolXml (); system ("pause "); return 1 ;}

 

Running result interface:

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.