下边我写了一个简单的测试程序,创建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,constchar*pszText)
- {
- TiXmlElementelmNode(pszNode);
- TiXmlTextelmText(pszText);
- XML_ASSERT(elmNode.InsertEndChild(elmText));
- XML_ASSERT(pElmParent->InsertEndChild(elmNode));
- returnRET_OK;
- }
- //getleafnode
- intGetLeafNode(TiXmlNode*pElmParent,char*pszNode,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)
- {
- returnRET_ERR;
- }
- if(pCats->iNum>0)
- {
- //num
- sprintf(szBuf,"%d",pCats->iNum);
- XML_RETURN(AddLeafNode(&elmRoot,XML_STR_CAT_NUM,szBuf));
- }
- else
- {
- returnRET_ERR;
- }
- 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
- sprintf(szBuf,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();
- returnRET_OK;
- }
- intReadXmlFromFile(OUTCat_List_Info*pCats)
- {
- TiXmlElement*pRootNode=NULL;
- TiXmlElement*pTemp=NULL;
- charszBuf[BUF_SIZE];
- szBuf[0]='/0';
- if(!pCats)
- {
- returnRET_ERR;
- }
- TiXmlDocumentxmlDoc(XML_STR_FILE);
- if(!xmlDoc.LoadFile())
- returnRET_ERR;
- XML_ASSERT(pRootNode=xmlDoc.RootElement());
- pTemp=pRootNode->FirstChildElement(XML_STR_CAT_NUM);
- if(pTemp)
- {
- GetLeafNode(pRootNode,szBuf);
- pCats->iNum=atoi(szBuf);
- }
- if(pCats->iNum>0)
- {
- TiXmlNode*pCat=NULL;
- pCat=pRootNode->FirstChild(XML_STR_CAT_INFO);
- if(pCat)
- {
- for(inti=0;i<pCats->iNum;i++)
- {
- //name
- GetLeafNode(pCat,pCats->pCatList[i].szName);
- //age
- GetLeafNode(pCat,szBuf);
- pCats->pCatList[i].iAge=atoi(szBuf);
- //color
- GetLeafNode(pCat,pCats->pCatList[i].szColor);
- pCat=pCat->NextSibling();
- }
- }
- }
- else
- {
- returnRET_ERR;
- }
- returnRET_OK;
- }
- intmain(intargc,char*argv[])
- {
- Cat_List_Infocats(true);
- cats.iNum=2;
- cats.pCatList=newCAT_INFO[cats.iNum];
- if(NULL==cats.pCatList)
- {
- cout<<"nomemory..."<<endl;
- returnRET_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");
- intiRet=WriteXmlToFile(&cats);
- iRet=ReadXmlFromFile(&cats);
- if(RET_OK==iRet)
- {
- for(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;
- }
- <?xmlversion="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>
从cat.xml文件读取节点元素,进行打印
原文链接:https://www.f2er.com/xml/300409.html