全部博文(776)
分类: C/C++
2012-09-26 19:05:41
使用Tinyxml创建和读取XML文件的优点:1,可在Windows和Linux中使用;2,方便易学,易用,可在获取源代码。将其中的文件tinystr.h,tinyxml.h,tinystr.cpp,tinyxmlerror.cpp,tinyxmlparser.cpp和tinyxml.cpp拷贝到您的工程目录,即可方便使用。
下边我写了一个简单的测试程序,创建XML文件,之后从该文件中读取XML节点元素。
#include
#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;
}
// TODO: Add extra validation here
TiXmlDocument *myDocument = new TiXmlDocument("lengquan.xml");
myDocument->LoadFile();
//获得根元素,
TiXmlElement *RootElement = myDocument->RootElement();
//输出根元素名称
//MessageBox(RootElement->Value());
TiXmlElement *FirstNode=RootElement->FirstChildElement();
//MessageBox(FirstNode->Value());
TiXmlElement *FirstChild=FirstNode->FirstChildElement();
//MessageBox(FirstChild->Value());
TiXmlElement *SecondChild=FirstChild->NextSiblingElement();
//MessageBox(SecondChild->GetText());
//获取属性 获取第一个属性值
TiXmlAttribute *AddrAttr=SecondChild->FirstAttribute();
//MessageBox(AddrAttr->Value());
//获取第二个属性值。
TiXmlAttribute *NoAttr=AddrAttr->Next();
MessageBox(NoAttr->Value());