使用TinyXml 遍历XML元素名,查找并返回指定元素

时间:2022-08-13 11:25:30

由于每次查找XML值的时候都要重写一次方法,感觉太麻烦了,所以就封装了一个方法来获取值。

TinyXml的东西就不贴了,用过的应该都能看懂。


方法一:递归方式

TiXmlElement* ReadElememt(TiXmlElement* InputElement, char* pName)
{
	TiXmlElement* ptemp = NULL;
	if (InputElement && 0 == strcmp(pName,InputElement->Value()))
	{
		printf("Find the element :%s \n", InputElement->Value());
		ptemp = InputElement;
		return ptemp;
	}
	else
	{
		printf("%s \n", InputElement->Value());
	}

	TiXmlElement* tmpElement = InputElement;
	if (tmpElement->FirstChildElement())
	{
		ptemp = ReadElememt(tmpElement->FirstChildElement(), pName);
	}
	if (!ptemp)
	{
		tmpElement = tmpElement->NextSiblingElement();
		if (tmpElement)
		{
			ptemp = ReadElememt(tmpElement ,  pName);
		}
	}
	return ptemp;
}

TiXmlElement SelectElementByName(char* InputInfo, char* pName, int iXMLType)
{
	//注:XMLTYPE 为1时,InputInfo为XML路径,当为2时,InputInfo为二进制文件内容
	TiXmlDocument cXmlDoc;
	TiXmlElement* pRootElement = NULL;
	if (iXMLType == 1)
	{
		if (!cXmlDoc.LoadFile(InputInfo))
		{
			printf("parse XML file failed \n");
			return TiXmlElement("");
		}
	}
	else if (iXMLType == 2)
	{
		if (!cXmlDoc.Parse(InputInfo))
		{
			printf("parse XML failed \n");
			return TiXmlElement("");
		}
	}

	pRootElement = cXmlDoc.RootElement();
	if (NULL == pRootElement)
	{
		printf("no have root Element\n");
		return TiXmlElement("");
	}
	else
	{
		TiXmlElement* pTempElement = NULL;
		pTempElement = ReadElememt(pRootElement, pName);
		if (pTempElement)
		{
			printf("find the Name : %s, Text = %s\n", pTempElement->Value(), pTempElement->GetText());
		}
		return *pTempElement;
	}	
}

long GetFile_size( char* filename )
{
	FILE* fp = fopen( filename, "r" );
	if( !fp ) { return -1; }
	fseek( fp, 0L, SEEK_END );
	long isize = ftell( fp );
	fclose( fp );

	return isize;
}

使用示例:

void main()
{
	char chFileName[MAX_PATH] = ".\\222.xml";
	long iFileSize = GetFile_size(chFileName);

	char* pFileBuffer = new char[iFileSize];

	FILE* xmlFile = fopen(chFileName, "rb");
	size_t iReadSize = 0;
	if (xmlFile)
	{
		iReadSize = fread(pFileBuffer, iFileSize, 1, xmlFile);
		fclose(xmlFile);
		xmlFile = NULL;
	}

	TiXmlElement pTempEle = SelectElementByName(".\\222.xml","PlateName" , 1);
	printf("find the Name : %s, Text = %s\n", pTempEle.Value(), pTempEle.GetText());




	if (pFileBuffer)
	{
		delete[] pFileBuffer;
		pFileBuffer = NULL;
	}
}


方法二:非递归方式


TiXmlElement ReadXmlByStack(char* InputInfo, char* pName,int iXMLType, bool& bFind)
{
	TiXmlDocument cXmlDoc;
	TiXmlElement* pRootElement = NULL;
	stack<TiXmlElement*> ElementStack;
	bFind = false;

	if (iXMLType == 1)
	{
		if (!cXmlDoc.LoadFile(InputInfo))
		{
			printf("parse XML file failed \n");
			return TiXmlElement("");
		}
	}
	else if (iXMLType == 2)
	{
		if (!cXmlDoc.Parse(InputInfo))
		{
			printf("parse XML failed \n");
			return TiXmlElement("");
		}
	}

	pRootElement = cXmlDoc.RootElement();
	if (NULL == pRootElement)
	{
		printf("no have root Element\n");
		return TiXmlElement("");
	}
	else
	{
		ElementStack.push(pRootElement);
		TiXmlElement* pTempElement = NULL;		
		while(ElementStack.size() > 0)
		{
			pTempElement = ElementStack.top();
			printf("%s\n", pTempElement->Value());

			ElementStack.pop();

			if (0 == strcmp(pTempElement->Value(), pName) )
			{
				bFind = true;
				break;
			}
			
			TiXmlElement* pTempSibLing = pTempElement->FirstChildElement();
			do
			{
				if (pTempSibLing)
				{
					ElementStack.push(pTempSibLing);
					pTempSibLing = pTempSibLing->NextSiblingElement();
				}
			}while(pTempSibLing);
		}

		while(ElementStack.size() > 0)
		{
			ElementStack.pop();
		}

		if (bFind)
		{			
			return *pTempElement;
		}
		else
		{
			return TiXmlElement("");
		}		
	}
}

使用示例:


void main()
{
	bool bFind = false;
	TiXmlElement pTempEle = ReadXmlByStack( ".\\222.xml", "PlateName",1, bFind);
	if (bFind)
	{
		printf("find the Name : %s, Text = %s\n", pTempEle.Value(), pTempEle.GetText());
	}	
}