Tinyxml解析xml优化(vc6.0调试ok)

TiXmlNode * FindChildNode(TiXmlNode * pNode,string strNodeName,string strNodeAttrName,string strNodeAttrValue)
{
	// 定义一个TiXmlDocument类指针
	TiXmlNode * pSelectNode = NULL;
	TiXmlNode * pChildNode = NULL;
	

	int t = pNode->Type();
	switch(t)
	{
	case TiXmlText::TINYXML_DOCUMENT:
	case TiXmlText::TINYXML_DECLARATION:
	case TiXmlText::TINYXML_TEXT:
	case TiXmlText::TINYXML_UNKNOWN:
	case TiXmlText::TINYXML_COMMENT:
		break;
	case TiXmlText::TINYXML_ELEMENT:
		if(pNode->Value() == strNodeName)
		{
			//cout << pNode->Value() << endl;
			if(!strNodeAttrName.empty() && !strNodeAttrValue.empty())
			{
				TiXmlElement * pElement = pNode->ToElement();
				
				TiXmlAttribute * pAttr = pElement->FirstAttribute();
				TiXmlAttribute * pNextAttr =NULL;
				if(pAttr != NULL)
				{	
					do
					{							
						if(pAttr->Name()==strNodeAttrName && pAttr->Value()== strNodeAttrValue)
						{
							cout << "Name: " << pAttr->Name();
							cout << " value: " << pAttr->Value() << endl;
							return pNode;
						}
					}while(pAttr = pAttr->Next());
				}
			}
			else
			{
				return pNode;
			}
			
		}
		
	default:
		break;
	}

	//循环访问它的每一个元素,否则找到第一个就不找了
	for(pChildNode=pNode->FirstChild();pChildNode!=0;pChildNode = pChildNode->NextSibling())
	{
		
		pSelectNode = FindChildNode(pChildNode,strNodeName,strNodeAttrName,strNodeAttrValue);
		if (pSelectNode)
		{
			break;
		}
			
	}
	
	return pSelectNode;
}

//cXmlName: xml文件名
//strNodeName: xml文件名
//strNodeAttrName1: xml文件名
//strNodeAttrValue1: xml文件名
//strNodeAttrName2: xml文件名
//根据xml中节点名、节点属性名及节点属性名对应的属性值唯一确定一节点
//返回值:获得的属性值,string 类型
TiXmlNode* GetOnlyOneNode(const char* cXmlName,string strNodeName,string strNodeAttrName,string strNodeAttrValue)
{
	//const char * cXmlName = "coverage.xml";
	//readXml(cXmlName);
// 	string strNodeName = "Folder";
// 	string strNodeAttrName = "Name";
// 	string strNodeAttrValue = "app1";
	string tmpAttrValue = "";

	TiXmlElement* RootElement = OpenXml(cXmlName);
	TiXmlNode * pSelectNode = NULL;
	TiXmlNode * pNode = NULL;

	for(pNode=RootElement->FirstChildElement();pNode;pNode=pNode->NextSiblingElement())
	{
		pSelectNode = FindChildNode(pNode,strNodeName,strNodeAttrName,strNodeAttrValue);
		if (pSelectNode)
		{
			break;
		}
	}

	return pSelectNode;
}

//从唯一指定的节点中指定该节点中一属性名,获得该属性名对应的属性值
string GetNodeAttrValue(TiXmlNode* pSelectNode, string strNodeAttrName)
{
	string strRtnNodeAttrValue = "";
	//获得其属性
	TiXmlElement * pElement = pSelectNode->ToElement();
	TiXmlAttribute * pAttr = pElement->FirstAttribute();
	TiXmlAttribute * pNextAttr =NULL;
	
	if(pAttr != NULL)
	{	
		string tmpAttrVal = "";
		string tmpAttrName = "";		
		
		do
		{							
			tmpAttrName = pAttr->Name();
			
			if (pAttr->Name()==strNodeAttrName)
			{
				strRtnNodeAttrValue = pAttr->Value();
				break;
			}
		}while(pAttr = pAttr->Next());
	}
	
	return strRtnNodeAttrValue;
}

int main(int argc,char* argv[])
{
	const char * cXmlName = "coverage.xml";
	string strNodeName = "Folder";
	string strNodeAttrName = "Name";
	string strNodeAttrValue = "app12";
	string strNodeAttrName1 = "value";

	TiXmlNode * pNode = GetOnlyOneNode(cXmlName,strNodeName,strNodeAttrName,strNodeAttrValue);
	string strNodeAttrValue1 = GetNodeAttrValue(pNode,strNodeAttrName1);

	cout << strNodeAttrValue1.c_str() << endl;

	return 0;
}

 

发现现在网上现有用tinyxml 工具用c、c++写的xml解析工具用起来有点不方便,如是自己优化优化了一下:

所解析的xml文档如下:

<?xml version="1.0" encoding="utf-8" ?>
<covergae Name="ne500e" statement="80%">
<Folder Name="release" path="D:\Release">
<Folder Name="product" path="D:\Release\product">
	<Folder Name="8090" path="D:\product\8090">
		<Folder Name="app1" path="D:\Release\product\8090\app1"/>
		<Folder Name="app2" path="D:\Release\product\8090\app2"/>
		<Folder Name="app3" path="D:\Release\product\8090\app3"/>
		<Folder Name="app4" path="D:\Release\product\8090\app4"/>
		<Folder Name="app5" path="D:\Release\product\8090\app5"/>
		<Folder Name="app6" path="D:\Release\product\8090\app6"/>
		<Folder Name="app7" path="D:\Release\product\8090\app7"/>
		<Folder Name="app8" path="D:\Release\product\8090\app8"/>
		<Folder Name="app9" path="D:\Release\product\8090\app9"/>
		<Folder Name="app10" path="D:\Release\product\8090\app10"/>
		<Folder Name="app11" path="D:\Release\product\8090\app11"/>
		<Folder Name="app12" path="D:\Release\product\8090\app12" value="1024"/>
	</Folder>
</Folder>
</Folder>
</covergae>

 

最后输出:

Name: Name value: app12
1024
Press any key to continue

你可能感兴趣的:(Tinyxml解析xml优化(vc6.0调试ok))