Parsing in C ++ XML: tinyxml

Source: Internet
Author: User

 Recently, tinyxml was used for C ++ XML parsing, which is easy to use. This article provides a simple example of using tinyxml for XML parsing, many complex applications can be completed based on the methods in this example. In future articles, we will explain examples of using xerces for C ++ XML parsing. I hope you can share them with us. </P> <p> tinyxml is an open-source parsing XML library that can be used in C ++ and compiled in Windows or Linux. The parsing library's model parses the XML file and then generates the DOM model in the memory, so that we can easily traverse this XML tree. <Br/> the DOM model is a Document Object Model that divides a document into multiple elements (such as books, chapters, sections, and segments ), the tree structure is used to represent the ordered relationship between these elements and the nested inclusion relationship. <Br/> first, download the tinyxml library from the Internet. The folder name is tinyxpath. Make the following configuration in the project: <br/> Add the file in the append include path: your tinyxpath path/tinyxpath/include <br/> Add in the path of the additional library: Your tinyxpath path/tinyxpath/lib <br/> Add in the object/library path: tinyxpathd. lib. If the release version is used, it is tinyxpath. lib. <Br/> In addition, because the project I developed is multi-threaded, a multi-threaded environment is set up, so there is no problem in using tinyxml. I wrote tinyxml in a separate C ++ project for testing, and found that if the multi-threaded environment is not set, a link error will occur. I think the reason may be that tinyxml uses a multi-threaded environment, so we need to set a multi-threaded environment. On the C/C ++ tab under project/settings, select code generation and select debug multithreaed DLL in use run-time library. <Br/> the XML file students in this example. XML is as follows: <class name = "computer software class"> </P> <p> <students> </P> <p> <Student name = "Zhang San" studentno = "13031001" sex = "male" age = "22"> </P> <p> <phone> 88208888 </phone> </P> <p> <address> No. 2 Taibai South Road, Xi'an </address> </P> <p> </student> </P> <p> <Student name = "" studentno = "13031002" Sex = "male" age = "20"> </P> <p> <phone> 88206666 </phone> </P> <p> <address> Guanghua Road, Xi'an </address> </P> <p> </student> </P> <p> </students> </P> <p> </class> <Br/> copy the code program code xmlparseexample. CPP is as follows: # include <iostream> </P> <p> # include <string> </P> <p> # include <tinyxml. h> </P> <p> Using STD: string; </P> <p> int main () </P> <p >{</P> <p> tixmldocument * mydocument = new tixmldocument (); </P> <p> mydocument-> LoadFile ("students. XML "); </P> <p> tixmlelement * rootelement = mydocument-> rootelement (); // class </P> <p> tixmlelement * studentselement = rootelement-> firstchildelement (); // Students </P> <p> tixmlelement * studentelement = studentselement-> firstchildelement (); // students </P> <p> while (studentelement) {</P> <p> tixmlattribute * attributeofstudent = studentelement-> firstattribute (); // obtain the name attribute of student </P> <p> while (attributeofstudent) {</P> <p> STD: cout <attributeofstudent-> name () <":" <attributeofstudent-> value () <STD: Endl; </P> <p> attributeofstudent = Ttributeofstudent-> next (); </P> <p >}</P> <p> tixmlelement * phoneelement = studentelement-> firstchildelement (); // obtain the phone element of student </P> <p> STD: cout <"phone" <":" <phoneelement-> gettext () <STD:: Endl; </P> <p> tixmlelement * addresselement = phoneelement-> nextsiblingelement (); </P> <p> STD :: cout <"Address" <":" <phoneelement-> gettext () <STD: Endl; </P> <p> studentelement = studentelement-> Nextsiblingelement (); </P> <p >}</P> <p> Delete mydocument; </P> <p> return 0; </P> <p >}< br/> the result of copying the code program is as follows: <br/> name: Zhang San <br/> studentno: 13031001 <br/> sex: male <br/> Age: 22 <br/> Phone: 88208888 <br/> address: 88208888 <br/> name: Li Si <br/> studentno: 13031002 <br/> Sex: Male <br/> Age: 20 <br/> Phone: 88206666 <br/> address: 88206666 </P> <p> in this example, XML files are parsed and easy to understand. However, many developers do not know how to parse XML streams (non-XML files) I have read tiny. The source code provided by XML, which can be parsed using the following method. The Code is as follows: string xmlstring = </P> <p> "<class name =/" computer software class/">/</P> <p> <students>/</P> <p> <Student name =/"James/" studentno =/"13031001/" Sex =/"male/" age =/"22/">/</P> <p> <phone> 88208888 </phone>/</P> <p> <address> No. 2 Taibai South Road, Xi'an </address>/</P> <p> </student> /</P> <p> <Student name =/"/" studentno =/"13031002/" Sex =/"male/" age =/"20/">/ </P> <p> <phone> 88206666 </phone>/</P> <p> <address> Guanghua Road, Xi'an </address>/</P> <p> </student >/</P> <p> </students>/</P> <p> </class> "; </P> <p> tixmldocument * mydocument = new tixmldocument (); </P> <p> mydocument-> parse (xmlstring. c_str (); <br/> copy the code and use the parse function to parse the XML Parse stream, which is unfamiliar to many developers. </P> <p> If a developer develops a specific application, the preceding method can be used. You do not need to process all attributes. For example, you can determine the attribute name, only the desired attributes or XML elements are processed. You can also use tinyxml to create XML elements and XML attributes, or set values corresponding to XML elements and attributes. If you want a similar example, you can leave a message. <Br/> The following describes some tinyxml classes. In tinyxml, some classes are defined based on various XML elements: <br/> tixmlbase: The base class of the entire tinyxml model. <Br/> tixmlattribute: attribute of the element in XML. <Br/> tixmlnode: corresponds to the node in the DOM structure. <Br/> tixmlcomment: corresponds to the comment in XML <br/> tixmldeclaration: corresponds to the declaration part in XML, <? Versiong = "1.0"?>. <Br/> tixmldocument: The entire XML document. <Br/> tixmlelement: Elements corresponding to XML. <Br/> tixmltext: text part of XML <br/> tixmlunknown: unknown part of XML. <Br/> tixmlhandler: defines some XML operations. <Br/>

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.