可以包含子节点和TiXmlAttributeTiXmlComment表示注释TiXmlDeclaration表示声明TiXmlText表示文本节点TiXmlUnknown表示未知节点通常是出错了TiXmlAttribute表示一个元素的属性各类之间的转换由于各个节点类都从TiXmlNode继承在使用时常常需要将TiXmlNode类型的指针转换为其派生类的指针在进行这种转换时应该首先使用由TiXmlNode类提供的一系列转换函数如ToElementvoid而不是c的dynamic_cast检查返回值由于TinyXml是一个非校验的解析器因此当解析一个文件时很可能文件并不包含我们预期的某个节点在这种情况下TinyXml将返回空指针。
因此必须要对返回值进行检查否则将很容易出现内存访问的错误。
如何重头建立一个XML文件先建立一个TiXmlDocument对象然后载入某个模板或者直接插入一个节点作为根节点接着就可以像打开一个已有的XML文件那样对它进行操作了。
三.TinyXml实践:Linux:MakefilesettingDEBUGcanbesettoYEStoincludedebugginginfoorNOotherwiseDEBUG:YESPROFILEcanbesettoYEStoincludeprofilinginfoorNOotherwisePROFILE:NOTINYXML_USE_STLcanbeusedtoturnonSTLsupport.NOthenSTLwillnotbeused.YESwillincludetheSTLfiles.TINYXML_USE_STL:YESTinyXml在构建时可以选择是否支持STL选择的话则可以使用std::string所以通常应该打开这个选项。
在Windows上TinyXml的源码包里提供了VC6的工程文件直接用它就可以生成两个静态库带STL和不带STL非常容易。
唯一需要注意的是默认生成的库是单线程的如果用在多线程的项目中需要改动一下配置生成相应的多线程库。
构建了相应的库之后在使用了它们的工程中只要在连接时把他们连上就行了。
需要注意的是如果需要STL支持在编译用到了TinyXml的文件时需要定义一个宏TIXML_USE_STL对gcc可以使用参数-DTIXML_USE_STL对cl.exeVC可以使用参数/DTIXML_USE_STL如果嫌麻烦可以直接定义在tinyxml.h文件里。
附件:pro四.例程:引自:http://www.cnblogs.com/phinecos/archive/2008/03/11/1100912.htmlCpp代码1.includeltiostreamgt2.includequottinyxml.hquot3.includequottinystr.hquot4.includeltstringgt5.includeltwindows.hgt6.includeltatlstr.hgt7.usingnamespacestd8.9.CStringGetAppPath10.//获取应用程序根目录11.12.TCHARmodulePathMAX_PATH13.GetModuleFileNameNULLmodulePathMAX_PATH14.CStringstrModulePathmodulePath15.strModulePathstrModulePath.LeftstrModulePath.ReverseFind_T16.returnstrModulePath17.18.19.boolCreateXmlFilestringampszFileName20.//创建xml文件szFilePath为文件保存的路径若创建成功返回true否则false21.22.try23.24.//创建一个XML的文档对象。
25.26.TiXmlDocumentmyDocumentnewTiXmlDocument27.//创建一个根元素并连接。
28.29.TiXmlElementRootElementnewTiXmlElementquotPersonsquot30.myDocument-gtLinkEndChildRootElement31.//创建一个Person元素并连接。
32.33.TiXmlElementPersonElementnewTiXmlElementquotPersonquot34.RootElement-gtLinkEndChildPersonElement35.//设置Person元素的属性。
36.37.PersonElement-gtSetAttributequotIDquotquot1quot38.//创建name元素、age元素并连接。
39.40.TiXmlElementNameElementnewTiXmlElementquotnamequot41.TiXmlElementAgeElementnewTiXmlElementquotagequot42.PersonElement-gtLinkEndChildNameElement43.PersonElement-gtLinkEndChildAgeElement44.//设置name元素和age元素的内容并连接。
45.46.TiXmlTextNameContentnewTiXmlTextquot周星星quot47.TiXmlTextAgeContentnewTiXmlTextquot22quot48.NameElement-gtLinkEndChildNameContent49.AgeElement-gtLinkEndChildAgeContent50.CStringappPathGetAppPath51.stringseperatorquotquot52.stringfullPathappPath.GetBuffer0seperatorszFileName53.myDocument-gtSaveFilefullPath.c_str//保存到文件54.55.56.catchstringampe57.58.returnfalse59.60.returntrue61.62.63.boolReadXmlFilestringampszFileName64.//读取Xml文件并遍历65.66.try67.68.CStringappPathGetAppPath69.stringseperatorquotquot70.stringfullPathappPath.GetBuffer0seperatorszFileName71.//创建一个XML的文档对象。
72.73.TiXmlDocumentmyDocumentnewTiXmlDocumentfullPath.c_str74.myDocument-gtLoadFile75.//获得根元素即Persons。
76.77.TiXmlElementRootElementmyDocument-gtRootElement78.//输出根元素名称即输出Persons。
79.80.coutltltRootElement-gtValueltltendl81.//获得第一个Person节点。
82.83.TiXmlElementFirstPersonRootElement-gtFirstChildElement84.//获得第一个Person的name节点和age节点和ID属性。
85.86.TiXmlElementNameElementFirstPerson-gtFirstChildElement87.TiXmlElementAgeElementNameElement-gtNextSiblingElement88.TiXmlAttributeIDAttributeFirstPerson-gtFirstAttribute89.//输出第一个Person的name内容即周星星age内容即ID属性即。
90.91.coutltltNameElement-gtFirstChild-gtValueltltendl92.coutltltAgeElement-gtFirstChild-gtValueltltendl93.coutltltIDAttribute-gtValueltltendl94.95.catchstringampe96.97.returnfalse98.99.returntrue100.101.intmain102.103.stringfileNamequotinfo.xmlquot104.CreateXmlFilefileName105.ReadXmlFilefileName106.107.本文来自CSDN博客转载请标明出处http://blog.csdn.net/qiaochenglei/archive/2010/04/12/5474231.aspxhttp://www.javaeye.com/wiki/topic/580229如下是一个XML片段ltPersonsgtltPersonIDquot1quotgtltnamegt周星.
上一篇:
【精品文献】朝龙学院、朝龙学院、威海企业职能培训、威海人才培训、易语言学习
下一篇:
商业银行发展绿色信贷业务的对策探讨