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);
	}
}

你可能感兴趣的:(String,null)