Tinyxml一一利用tinyxml写入节点元素

接下来我们需要修改Test.xml文件,改变文件中节点元素值,如下所示,我们需要修改Text1值的元素值为wt1:


    
    
    t1
    t2

实现代码如下:

/*

	
	
	wt1
	t2

*/
bool CTinyxmlEngine::Write_XML_1Floor_Text(const char*pszPath,const char*pszName,const char*pszValue)
{
	TiXmlDocument doc(pszPath);
	if (!doc.LoadFile())
	{
		return false;
	}
	const char*pszXmlName;
	TiXmlElement*p_root = doc.RootElement();
	for (TiXmlNode*p_node = p_root->FirstChildElement();p_node;p_node=p_node->NextSiblingElement())
	{
		pszXmlName = p_node->Value();
		if (strcmp(pszXmlName,pszName)==0)
		{
			TiXmlNode*node =p_node->FirstChild();
			TiXmlText text(pszValue);
			p_node->ReplaceChild(node,text);
			doc.SaveFile();
			return true;
		}
	}
	return false;
}

接下来我们尝试修改二级节点元素值,只需按照一级节点的修改方式向下增加一次遍历即可,如下所示:

/*

	
	
		t1
		t2
	

*/
bool CTinyxmlEngine::Write_XML_2Floor_Text(const char*pszPath,const char*pszNode,const char*pszName,const char*pszValue)
{
	TiXmlDocument doc(pszPath); 
	if (!doc.LoadFile()) 
	{      
		return false;
	}
	const char*pszXmlName;
	TiXmlElement*p_root = doc.RootElement();
	for (TiXmlNode*p_node = p_root->FirstChildElement();p_node;p_node=p_node->NextSiblingElement())
	{
		pszXmlName=p_node->Value();
		if (strcmp(pszXmlName,pszNode)==0)
		{
			TiXmlElement*p_element = p_node->ToElement();
			for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
			{
				pszXmlName=p_node->Value();
				if (strcmp(pszXmlName,pszName)==0)
				{
					TiXmlNode*node =p_node->FirstChild();
					TiXmlText text(pszValue);
					p_node->ReplaceChild(node,text);
					doc.SaveFile();
					return true;
				}
			}
		}
	}
	return false;
}

同理,修改三级节点元素值:

/*

    
    
        
            t1
            t2
        
        
            t1
            t2
        
    

*/
bool CTinyxmlEngine::Write_XML_3Floor_Text(const char*pszPath,const char*pszNode,const char*pszNode1,const char*pszName,const char*pszValue)
{
	TiXmlDocument doc(pszPath); 
	if (!doc.LoadFile()) 
	{      
		return false;
	}
	const char*pszXmlName;
	TiXmlElement*p_root = doc.RootElement();
	for (TiXmlNode*p_node = p_root->FirstChildElement();p_node;p_node=p_node->NextSiblingElement())
	{
		pszXmlName=p_node->Value();
		if (strcmp(pszXmlName,pszNode)==0)
		{
			TiXmlElement*p_element = p_node->ToElement();
			for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
			{
				pszXmlName=p_node->Value();
				if (strcmp(pszXmlName,pszNode1)==0)
				{
					TiXmlElement*p_element = p_node->ToElement();
					for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
					{
						pszXmlName=p_node->Value();
						if (strcmp(pszXmlName,pszName)==0)
						{
							TiXmlNode*node =p_node->FirstChild();
							TiXmlText text(pszValue);
							p_node->ReplaceChild(node,text);
							doc.SaveFile();
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}

修改四级节点元素值:

/*

    
    
        
            
                t1
                t2
            
            
                t1
                t2
            
        
	

*/
bool CTinyxmlEngine::Write_XML_4Floor_Text(const char*pszPath,const char*pszNode,const char*pszNode1,const char*pszNode2,const char*pszName,const char*pszValue)
{
	TiXmlDocument doc(pszPath); 
	if (!doc.LoadFile()) 
	{      
		return false;
	}
	const char*pszXmlName;
	TiXmlElement*p_root = doc.RootElement();
	for (TiXmlNode*p_node = p_root->FirstChildElement();p_node;p_node=p_node->NextSiblingElement())
	{
		pszXmlName=p_node->Value();
		if (strcmp(pszXmlName,pszNode)==0)
		{
			TiXmlElement*p_element = p_node->ToElement();
			for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
			{
				pszXmlName=p_node->Value();
				if (strcmp(pszXmlName,pszNode1)==0)
				{
					TiXmlElement*p_element = p_node->ToElement();
					for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
					{
						pszXmlName=p_node->Value();
						if (strcmp(pszXmlName,pszNode2)==0)
						{
							TiXmlElement*p_element = p_node->ToElement();
							for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
							{
								pszXmlName=p_node->Value();
								if (strcmp(pszXmlName,pszName)==0)
								{
									TiXmlNode*node =p_node->FirstChild();
									TiXmlText text(pszValue);
									p_node->ReplaceChild(node,text);
									doc.SaveFile();
									return true;
								}
							}
						}
					}
				}
			}
		}
	}
	return false;
}

函数调用如下:

int main()
{
	CTinyxmlEngine m_engine;
	m_engine.Write_XML_1Floor_Text("Test.xml","Text1","wt1");
	m_engine.Write_XML_2Floor_Text("Test.xml","Floor1","Text1","wt2");
	m_engine.Write_XML_3Floor_Text("Test.xml","Floor2","Floor1_1","Text1","wt3");
	m_engine.Write_XML_4Floor_Text("Test.xml","Floor3","Floor2_1","Floor1_1","Text1","wt4");
	return 0;
}

修改完成后的Test.xml文件如下所示:



    
    
    wt1
    t2
    
    
        wt2
        t2
    
    
    
        
            wt3
            t2
        
        
            t1
            t2
        
    
    
    
        
            
                wt4
                t2
            
            
                t1
                t2
            
        
        
            
                t1
                t2
            
            
                t1
                t2
            
        
    

 

你可能感兴趣的:(C++,Tinyxml)