使用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中测试通过。
- #include<iostream>
- #include"tinyxml.h"
- usingnamespacestd;
- #pragmawarning(disable:4996)
- #defineXML_STR_FILE"cat.xml"
- #defineXML_STR_ROOT"xml-root"
- #defineXML_STR_CAT_INFO"cat-info"
- #defineXML_STR_CAT_NUM"num"
- #defineXML_STR_CAT_AGE"age"
- #defineXML_STR_CAT_COLOR"color"
- #defineXML_STR_CAT_NAME"name"
- #defineIN
- #defineOUT
- #defineRET_OK0
- #defineRET_ERR-1
- #defineNAME_LENGTH20
- #defineCOLOR_LENGTH20
- #defineBUF_SIZE32
- #defineXML_RETURN(x){if(RET_OK!=(x))returnRET_ERR;}
- #defineXML_ASSERT(x){if(NULL==(x))returnRET_ERR;}
- structCAT_INFO
- {
- CAT_INFO():iAge(0)
- szName[0]='/0';
- szColor[0]='/0';
- };
- intiAge;//age
- charszName[NAME_LENGTH];//name
- charszColor[COLOR_LENGTH];//color
- typedefCAT_INFO*PCAT_INFO;
- classCat_List_Info
- public:
- Cat_List_Info(boolbDel):bIsDelete(bDel),iNum(0),pCatList(NULL)
- {
- ~Cat_List_Info()
- if(bIsDelete&&pCatList)
- delete[]pCatList;
- pCatList=NULL;
- }
- public:
- intiNum;//catsnumber
- PCAT_INFOpCatList;//list
- private:
- boolbIsDelete;
- //addleafnode
- intAddLeafNode(TiXmlNode*pElmParent,constchar*pszNode,87); background-color:inherit; font-weight:bold">char*pszText)
- TiXmlElementelmNode(pszNode);
- TiXmlTextelmText(pszText);
- XML_ASSERT(elmNode.InsertEndChild(elmText));
- XML_ASSERT(pElmParent->InsertEndChild(elmNode));
- returnRET_OK;
- //getleafnode
- intGetLeafNode(TiXmlNode*pElmParent,char*pszText)
- TiXmlNode*pTemp;
- if(pElmParent&&(pTemp=pElmParent->FirstChild(pszNode)))
- if(pTemp=pTemp->FirstChild())
- strcpy(pszText,pTemp->Value());
- returnRET_OK;
- }
- returnRET_ERR;
- intWriteXmlToFile(INCat_List_Info*pCats)
- TiXmlDeclarationDeclaration("1.0","","");
- TiXmlDocumentxmlDoc(XML_STR_FILE);
- xmlDoc.InsertEndChild(Declaration);
- TiXmlElementelmRoot(XML_STR_ROOT);
- charszBuf[BUF_SIZE];
- szBuf[0]='/0';
- if(!pCats)
- if(pCats->iNum>0)
- //num
- sprintf(szBuf,"%d",pCats->iNum);
- XML_RETURN(AddLeafNode(&elmRoot,XML_STR_CAT_NUM,szBuf));
- else
- for(inti=0;i<pCats->iNum;i++)
- TiXmlElementelmCat(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
- if('/0'!=pCats->pCatList[i].szColor[0])
- XML_ASSERT(elmRoot.InsertEndChild(elmCat));
- XML_ASSERT(xmlDoc.InsertEndChild(elmRoot));
- //save
- xmlDoc.SaveFile();
- intReadXmlFromFile(OUTCat_List_Info*pCats)
- TiXmlElement*pRootNode=NULL;
- TiXmlElement*pTemp=NULL;
- TiXmlDocumentxmlDoc(XML_STR_FILE);
- if(!xmlDoc.LoadFile())
- XML_ASSERT(pRootNode=xmlDoc.RootElement());
- pTemp=pRootNode->FirstChildElement(XML_STR_CAT_NUM);
- if(pTemp)
- GetLeafNode(pRootNode,szBuf);
- pCats->iNum=atoi(szBuf);
- TiXmlNode*pCat=NULL;
- pCat=pRootNode->FirstChild(XML_STR_CAT_INFO);
- if(pCat)
- GetLeafNode(pCat,pCats->pCatList[i].szName);
- pCats->pCatList[i].iAge=atoi(szBuf);
- GetLeafNode(pCat,pCats->pCatList[i].szColor);
- pCat=pCat->NextSibling();
- else
- returnRET_ERR;
- intmain(intargc,87); background-color:inherit; font-weight:bold">char*argv[])
- Cat_List_Infocats(true);
- cats.iNum=2;
- cats.pCatList=newCAT_INFO[cats.iNum];
- if(NULL==cats.pCatList)
- cout<<"nomemory..."<<endl;
- 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");
- intiRet=WriteXmlToFile(&cats);
- iRet=ReadXmlFromFile(&cats);
- if(RET_OK==iRet)
- inti=0;i<cats.iNum;i++)
- cout<<i<<"cat'snameis"<<cats.pCatList[i].szName<<endl;
- cout<<i<<"cat'sageis"<<cats.pCatList[i].iAge<<endl;
- cout<<i<<"cat'scoloris"<<cats.pCatList[i].szColor<<endl;
- system("pause");
- return0;
- }
[xhtml]
copy