TinyXML usage, used to parse GPX files

Source: Internet
Author: User

TinyXML is an open source C++xml analytic toolset that is simple, lightweight and efficient, so it's a great choice for working with XML files.

Because it is open source, so it can be easily downloaded for free, Baidu is easy to find, there is not much to say.

After downloading, you will get a compressed package that contains a lot of. h and. cpp files, just copy six of them into your own project folder:

Tinyxml.h,

Tinystr.h,

Tinyxml.cpp,

Tinyxmlerror.cpp,

Tinyxmlparser.cpp,

Tinystr.cpp

If you use VS as a development tool, you also need to include the #include "stdafx.h" statement in the CPP file for the above copy .

Then, in the place where TinyXML is used, include: #include "tinyxml.h" #include "tinystr.h"

The next step is the development section, the TINYXML structure is as follows:

Here we give a simple example to illustrate its usage, this method uses TinyXML to parse GPX files. GPX is an XML document used to store GPS data and is an international standard, with the following analytic methods:

1Statecode READTRACKFROMGPX (track* track,Char*Path)2 {3cout<<"Start parsing gpx file ..."<<Endl;4      //reading GPX Files5 tixmldocument xmldoc (Path);6 xmldoc.loadfile ();7      //pointer to the element of the root node8tixmlelement*xmlrootelement;9      //pointer to the element of the child nodeTentixmlelement*xmlsubelement; One      //Node Properties Atixmlattribute*rootattr; -     //tixmlnode* pnode = NULL; -  the      //Get root node -Xmlrootelement =xmldoc.rootelement (); -     if(Xmlrootelement = =NULL) -     { +         return 1; -     } +     //cout<< "root element is:" <<xmlrootelement->value () <<endl; A     //properties of the root node atRootattr = xmlrootelement->FirstAttribute (); -     //cout<< "There Is it attributes:" <<endl; -      while(rootattr!=NULL) -     { -           //cout<<rootattr->name () << ":" <<rootattr->value () <<endl; -Rootattr = rootattr->Next (); in     } -  toXmlsubelement = xmlrootelement->firstchildelement (); +     if(Xmlsubelement = =NULL) -     { the         returnGpx_file_empty;//GPX file is empty *     } $      while(xmlsubelement!=NULL)Panax Notoginseng     { -               the             if(strcmp (Xmlsubelement->value (),"trk")==0) +                     Break; A             Else theXmlsubelement = xmlsubelement->nextsiblingelement (); +     } -     if(Xmlsubelement = =NULL) $     { $         returnGpx_file_track_noexit;//No trace data exists in GPX file -     } -     //cout<< "Next is the trajectory data:" <<endl; the  -Xmlsubelement = xmlsubelement->firstchildelement ();Wuyi      while(xmlsubelement!=NULL) the     { -          if(strcmp (Xmlsubelement->value (),"trkseg")!=0) Wu              //output name and number -Cout<<xmlsubelement->value () <<":"<<xmlsubelement->gettext () <<Endl; About          Else $               Break; -Xmlsubelement = xmlsubelement->nextsiblingelement (); -     } -  A     if(Xmlsubelement = =NULL) +     { the         returnGpx_file_track_noexit;//No trace data exists in GPX file -     } $  theXmlsubelement = xmlsubelement->firstchildelement (); theRootattr = xmlsubelement->FirstAttribute (); thetixmlelement* xmlnode=NULL; the      while(xmlsubelement!=NULL) -     { in TrackPoint OnePoint; the          while(rootattr!=NULL) the         { About             //latitude and longitude of output the             //cout<<rootattr->name () << ":" <<rootattr->value () <<endl;  the             if(strcmp (Rootattr->name (),"lat")==0) theOnepoint.lat = stringtonum<Double> (rootattr->Value ()); +             if(strcmp (Rootattr->name (),"Lon")==0) -Onepoint.lon = stringtonum<Double> (rootattr->Value ()); theRootattr = rootattr->Next ();Bayi          } theXmlNode = xmlsubelement->firstchildelement (); the          while(xmlnode!=NULL) -         { -             //elevation (not in some GPX files) and time the             //cout<<xmlnode->value () << ":" <<xmlnode->gettext () <<endl; the             if(strcmp (Xmlnode->value (),"Ele")==0) theOnepoint.ele = stringtonum<Double> (xmlnode->GetText ()); the             if(strcmp (Xmlnode->value ()," Time")==0) -Onepoint.time = xmlnode->GetText (); theXmlNode = xmlnode->nextsiblingelement (); the         } the 94Xmlsubelement = xmlsubelement->nextsiblingelement (); the         if(xmlsubelement!=NULL) theRootattr = xmlsubelement->FirstAttribute (); the 98Track->Addpoint (OnePoint); About     }  -Track->setstarttime (Track->getpointset () [0].time);//Get start time101Track->setendtime (Track->getpointset (). Back (). time);//Get end Time102cout<<"Parse End ...";103 xmldoc.clear ();104     returnGpx_file_paser_succeed; the}

Track is used to store trace data in code.

TinyXML usage, used to parse GPX files

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.