TinyXML 根據屬性名,屬性值返回對應的元素節點(源代碼)

遞歸實現 根據元素屬性名,屬性值,返回對應的元素節點。詳情看代碼。

 

//遞歸所有節點
TiXmlElement*  RecursionAllNode(TiXmlElement * pElement,string attributName,string attributValue)
{
	string  strValue;
	TiXmlElement* retValue;
	if (pElement== NULL)
	{
		return NULL;
	}else if (pElement->NoChildren()) 
	{
		pElement->QueryValueAttribute(attributName,&strValue);
		if (attributValue==strValue)
		{

			return pElement;
		}

	 return	RecursionAllNode(NULL,attributName,attributValue);
	}else if (!pElement->NoChildren())
	{

		pElement->QueryValueAttribute(attributName,&strValue);
		if (attributValue==strValue)
		{
			cout<<pElement->Value()<<endl;

			return pElement;
		}
		TiXmlElement * pChilds = pElement->FirstChildElement();//第一個子結點

		retValue=RecursionAllNode(pChilds,attributName,attributValue);
		if (retValue!=NULL)
		{
			return retValue;
		}
		//遞歸子結點
		pChilds = pChilds->NextSiblingElement();
		while ( NULL != pChilds )//遞歸處理此結點下的所有結點
		{
			retValue=RecursionAllNode(pChilds,attributName,attributValue);
			if (retValue!=NULL)
			{
				return retValue;
			}
			pChilds = pChilds->NextSiblingElement();
		}
		return RecursionAllNode(NULL,attributName,attributValue);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章