您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > TinyXml之简单应用
TinyXMLTinyXMLTinyXmlforC++XMLTTiinnyyXXMMLLTinyXmlclassxmlXML?xmlversion=1.0?!--Thisiscommenthere.--DataPlayerlevel=12Kasi/PlayerPlayerGosla/Player/DataTinyXmlTiXmlDocumentdemo.xmlTiXmlDeclarationversion='1.0'TiXmlCommentThisiscommenthere.TiXmlElementDataTiXmlElementPlayerAttribtutes:level=12TiXmlTextKasiTiXmlElementPlayerTiXmlTextGosla?xmlversion=1.0?TiXmlDeclarationTiXmlDocumentchildTiXmlUnknownCopyright©2005,9FStudioTinyXML!--Thisiscommenthere.--TiXmlCommentDataTiXmlElementattributechildPlayerPlayerlevel=12DatachildTiXmlElementlevel,12KasiPlayerlevel=12childTiXmlTextleafnodechildTinyXMLXML//sample/demotest.cpp#includestdlib.h#includestdio.h#includetinyxml.h//XMLfilecontentconstchar*demoStart=?xmlversion=\1.0\?\n!--Thisiscommenthere.--\nData\nPlayerlevel=\12\Kasi/Player\nPlayerGosla/Player\n/Data\n;intmain(){//Writetoafileandreaditback,tocheckfileI/O.TiXmlDocumentOutputDoc(demotest.xml);OutputDoc.Parse(demoStart);if(OutputDoc.Error()){printf(Errorin%s:%s\n,Copyright©2005,9FStudioTinyXMLOutputDoc.Value(),OutputDoc.ErrorDesc());exit(1);}OutputDoc.SaveFile();TiXmlDocumentInputDoc(demotest.xml);boolloadOkay=InputDoc.LoadFile();if(!loadOkay){printf(Couldnotload'demotest.xml':Error='%s'.\n,InputDoc.ErrorDesc());exit(1);}return0;}TiXmlDocumentInputDoc;boolloadOkay=InputDoc.LoadFile(demotest.xml);XMLTinyXMLXMLdemo.xml?xmlversion=1.0?Data!--Thisiscommenthere.--Playerlevel=12rate=0.2desc='Greatguy!'Kasi/PlayerPlayerNameGosla/NameLevel13/LevelRate0.3/RateDescWonderfulguy!/Desc/Player/Data//sample/readdata.cppCopyright©2005,9FStudioTinyXML#includestdlib.h#includestdio.h#includestring#includetinyxml.hintmain(){TiXmlDocumentdoc;boolloadOkay=doc.LoadFile(demo.xml);if(!loadOkay){printf(Couldnotload'demo.xml'.Error='%s'.\n,doc.ErrorDesc());exit(1);}//--------------------------------------//begintoreaddata//--------------------------------------TiXmlNode*node=0;TiXmlElement*dataElement=0;TiXmlElement*itemElement=0;TiXmlComment*itemComment=0;TiXmlText*itemText=0;intlevel=0;doublerate=0;std::stringname,desc;//GettheDataelement.//Itisachildofthedocument,andcanbeselectedbyname.node=doc.FirstChild(Data);dataElement=node-ToElement();//Getthecommentnode=dataElement-FirstChild();itemComment=node-ToComment();Copyright©2005,9FStudioTinyXMLprintf(comment:%s\n,itemComment-Value());//GetthefirstPlayer//UsingFirstChildElementcanskipthecomment.node=dataElement-FirstChildElement();itemElement=node-ToElement();itemText=itemElement-FirstChild()-ToText();//queryattributesitemElement-QueryIntAttribute(level,&level);itemElement-QueryDoubleAttribute(rate,&rate);desc=itemElement-Attribute(desc);name=itemText-Value();//Outputinfomationprintf(name:%s,level:%d,rate:%f,desc:%s\n,name.c_str(),level,rate,desc.c_str());//GetthesecondPlayer//IMPORTANT:It'sitemElement::NextSiblingElement()//beencalled,notdataElement's.itemElement=itemElement-NextSiblingElement();//GetnameitemElement=itemElement-FirstChildElement();itemText=itemElement-FirstChild()-ToText();name=itemText-Value();//GetlevelitemElement=itemElement-NextSiblingElement();itemText=itemElement-FirstChild()-ToText();level=atoi(itemText-Value());//GetrateitemElement=itemElement-NextSiblingElement();itemText=itemElement-FirstChild()-ToText();rate=atof(itemText-Value());//GetdescitemElement=itemElement-NextSiblingElement();itemText=itemElement-FirstChild()-ToText();desc=itemText-Value();Copyright©2005,9FStudioTinyXML//Outputinfomationprintf(name:%s,level:%d,rate:%f,desc:%s\n,name.c_str(),level,rate,desc.c_str());return0;}comment:Thisiscommenthere.name:Kasi,level:12,rate:0.200000,desc:Greatguy!name:Gosla,level:13,rate:0.300000,desc:Wonderfulguy!nodechildnode=doc.FirstChild(Data);dataElement=node-ToElement();Datachild?xmlversion=1.0?child//childfor(node=parentNode.FirstChild();node;node=node-NextSibling()){//dosomething}//testchildfor(node=parentNode.FirstChild(“test”);node;node=node-NextSibling()){//dosomething}//TiXmlElementchildfor(element=parentElement-FirstChildElement();element;element=element-NextSiblingElement()){Copyright©2005,9FStudioTinyXML//dosomething}XMLPlayerlevel=12rate=0.2desc='Greatguy!'Kasi/PlayerPlayerNameGosla/NameLevel13/LevelRate0.3/RateDescWonderfulguy!/Desc/PlayerTinyXMLXMLTiXmlElement//queryattributesitemElement-QueryIntAttribute(level,&level);itemElement-QueryDoubleAttribute(rate,&rate);desc=itemElement-Attribute(desc);intdouble//Getname...name=itemText-Value();//Getlevel...level=atoi(itemText-Value());////Getrate...rate=atof(itemText-Value());//stringstd::stringTiXmlNodeValue()sub-classDocument:filenameofthexmlfileElement:nameoftheelementComment:thecommenttextUnknown:thetagcontentsText:thetextstringCopyright©2005,9FStudioTinyXMLXMLelement?xmlversion=1.0?Data!--Thisiscommenthere.--Playerlevel=12rate=0.2desc='Greatguy!'Kasi/PlayerPlayerNameGosla/NameLevel13/LevelRate0.3/RateDescWonderfulguy!/Desc
本文标题:TinyXml之简单应用
链接地址:https://www.777doc.com/doc-7880377 .html