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: