使用Tinyxml创建和读取XML文件的优点:1,可在Windows和Linux中使用;2,方便易学,易用,可在http://sourceforge.net/projects/tinyxml/获取源代码。将其中的文件tinystr.h,tinyxml.h,tinystr.cpp,tinyxmlerror.cpp,tinyxmlparser.cpp和tinyxml.cpp拷贝到您的工程目录,即可方便使用。
下边我写了一个简单的测试程序,创建XML文件,之后从该文件中读取XML节点元素。测试程序在VS2005中测试通过。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
#include <iostream> #include "tinyxml.h" using namespace std; #pragma warning(disable:4996) #define XML_STR_FILE "cat.xml" #define XML_STR_ROOT "xml-root" #define XML_STR_CAT_INFO "cat-info" #define XML_STR_CAT_NUM "num" #define XML_STR_CAT_AGE "age" #define XML_STR_CAT_COLOR "color" #define XML_STR_CAT_NAME "name" #define IN #define OUT #define RET_OK 0 #define RET_ERR -1 #define NAME_LENGTH 20 #define COLOR_LENGTH 20 #define BUF_SIZE 32 #define XML_RETURN(x) { if( RET_OK != (x) ) return RET_ERR; } #define XML_ASSERT(x) { if( NULL == (x) ) return RET_ERR; } struct CAT_INFO { CAT_INFO():iAge(0) { szName[0] = '/0'; szColor[0] = '/0'; }; int iAge; //age char szName[NAME_LENGTH]; //name char szColor[COLOR_LENGTH]; //color }; typedef CAT_INFO * PCAT_INFO; class Cat_List_Info { public: Cat_List_Info( bool bDel ):bIsDelete( bDel ), iNum( 0 ), pCatList( NULL ) { }; ~Cat_List_Info() { if( bIsDelete && pCatList ) { delete []pCatList; pCatList = NULL; } }; public: int iNum; //cats number PCAT_INFO pCatList; //list private: bool bIsDelete; }; //add leaf node int AddLeafNode( TiXmlNode* pElmParent, const char* pszNode, const char* pszText ) { TiXmlElement elmNode(pszNode); TiXmlText elmText(pszText); XML_ASSERT( elmNode.InsertEndChild( elmText ) ); XML_ASSERT( pElmParent->InsertEndChild( elmNode ) ); return RET_OK; } //get leaf node int GetLeafNode( TiXmlNode* pElmParent, char* pszNode, char* pszText ) { TiXmlNode *pTemp; if ( pElmParent && ( pTemp = pElmParent->FirstChild( pszNode ) ) ) { if ( pTemp = pTemp->FirstChild() ) { strcpy( pszText, pTemp->Value() ); return RET_OK; } } return RET_ERR; } int WriteXmlToFile( IN Cat_List_Info *pCats ) { TiXmlDeclaration Declaration( "1.0","", "" ); TiXmlDocument xmlDoc( XML_STR_FILE ); xmlDoc.InsertEndChild( Declaration ); TiXmlElement elmRoot( XML_STR_ROOT ); char szBuf[BUF_SIZE]; szBuf[0] = '/0'; if ( !pCats ) { return RET_ERR; } if ( pCats->iNum > 0) { //num sprintf( szBuf,"%d", pCats->iNum ); XML_RETURN( AddLeafNode( &elmRoot, XML_STR_CAT_NUM, szBuf ) ); } else { return RET_ERR; } for ( int i = 0; i < pCats->iNum; i++ ) { TiXmlElement elmCat( XML_STR_CAT_INFO ); //name if ( '/0' != pCats->pCatList[i].szName[0] ) { XML_RETURN( AddLeafNode( &elmCat, XML_STR_CAT_NAME, pCats->pCatList[i].szName ) ); } //age sprintf( szBuf,"%d", pCats->pCatList[i].iAge ); XML_RETURN( AddLeafNode( &elmCat, XML_STR_CAT_AGE, szBuf ) ); //color if ( '/0' != pCats->pCatList[i].szColor[0] ) { XML_RETURN( AddLeafNode( &elmCat, XML_STR_CAT_COLOR, pCats->pCatList[i].szColor ) ); } XML_ASSERT( elmRoot.InsertEndChild( elmCat ) ); } XML_ASSERT( xmlDoc.InsertEndChild( elmRoot ) ); //save xmlDoc.SaveFile(); return RET_OK; } int ReadXmlFromFile( OUT Cat_List_Info *pCats ) { TiXmlElement *pRootNode = NULL; TiXmlElement *pTemp = NULL; char szBuf[BUF_SIZE]; szBuf[0] = '/0'; if (!pCats) { return RET_ERR; } TiXmlDocument xmlDoc( XML_STR_FILE ); if( !xmlDoc.LoadFile() ) return RET_ERR; XML_ASSERT( pRootNode = xmlDoc.RootElement() ); pTemp = pRootNode->FirstChildElement( XML_STR_CAT_NUM ); if ( pTemp ) { GetLeafNode( pRootNode, XML_STR_CAT_NUM, szBuf ); pCats->iNum = atoi( szBuf ); } if ( pCats->iNum > 0 ) { TiXmlNode *pCat = NULL; pCat = pRootNode->FirstChild( XML_STR_CAT_INFO ); if ( pCat ) { for ( int i = 0; i < pCats->iNum; i++ ) { //name GetLeafNode( pCat, XML_STR_CAT_NAME, pCats->pCatList[i].szName ); //age GetLeafNode( pCat, XML_STR_CAT_AGE, szBuf ); pCats->pCatList[i].iAge = atoi(szBuf); //color GetLeafNode( pCat, XML_STR_CAT_COLOR, pCats->pCatList[i].szColor ); pCat = pCat->NextSibling(); } } } else { return RET_ERR; } return RET_OK; } int main(int argc, char* argv[]) { Cat_List_Info cats(true); cats.iNum = 2; cats.pCatList = new CAT_INFO[cats.iNum]; if ( NULL == cats.pCatList ) { cout<<"no memory..."<<endl; return RET_ERR; } cats.pCatList[0].iAge = 5; strcpy( cats.pCatList[0].szColor, "white" ); strcpy( cats.pCatList[0].szName, "Bob" ); cats.pCatList[1].iAge = 6; strcpy( cats.pCatList[1].szColor, "black" ); strcpy( cats.pCatList[1].szName, "Tom" ); int iRet = WriteXmlToFile( &cats ); iRet = ReadXmlFromFile( &cats ); if ( RET_OK == iRet ) { for ( int i = 0; i < cats.iNum; i++ ) { cout<<i<<" cat's name is "<<cats.pCatList[i].szName<<endl; cout<<i<<" cat's age is "<<cats.pCatList[i].iAge<<endl; cout<<i<<" cat's color is "<<cats.pCatList[i].szColor<<endl; } } system( "pause" ); return 0; } |
下边是生成的cat.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" ?> <xml-root> <num>2</num> <cat-info> <name>Bob</name> <age>5</age> <color>white</color> </cat-info> <cat-info> <name>Tom</name> <age>6</age> <color>black</color> </cat-info> </xml-root> |
发表评论
要发表评论,您必须先登录。