遞歸實現 根據元素屬性名,屬性值,返回對應的元素節點。詳情看代碼。
//遞歸所有節點
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);
}
}