接下来我们需要修改Test.xml文件,改变文件中节点元素值,如下所示,我们需要修改Text1值的元素值为wt1:
<FileRoot>
<!--Setting for FileRoot-->
<!--一级节点-->
<Text1>t1</Text1>
<Text2>t2</Text2>
</FileRoot>
实现代码如下:
/*
<FileRoot>
<!--Setting for FileRoot-->
<!--一级节点-->
<Text1>wt1</Text1>
<Text2>t2</Text2>
</FileRoot>
*/
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;
}
接下来我们尝试修改二级节点元素值,只需按照一级节点的修改方式向下增加一次遍历即可,如下所示:
/*
<FileRoot>
<!--二级节点-->
<Floor1>
<Text1>t1</Text1>
<Text2>t2</Text2>
</Floor1>
</FileRoot>
*/
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;
}
同理,修改三级节点元素值:
/*
<FileRoot>
<!--三级节点-->
<Floor2>
<Floor1_1>
<Text1>t1</Text1>
<Text2>t2</Text2>
</Floor1_1>
<Floor1_2>
<Text1>t1</Text1>
<Text2>t2</Text2>
</Floor1_2>
</Floor2>
</FileRoot>
*/
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;
}
修改四级节点元素值:
/*
<FileRoot>
<!--四级节点-->
<Floor3>
<Floor2_1>
<Floor1_1>
<Text1>t1</Text1>
<Text2>t2</Text2>
</Floor1_1>
<Floor1_2>
<Text1>t1</Text1>
<Text2>t2</Text2>
</Floor1_2>
</Floor2_1>
</Floor3>
</FileRoot>
*/
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文件如下所示:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<FileRoot>
<!--Setting for FileRoot-->
<!--一级节点-->
<Text1>wt1</Text1>
<Text2>t2</Text2>
<!--二级节点-->
<Floor1>
<Text1>wt2</Text1>
<Text2>t2</Text2>
</Floor1>
<!--三级节点-->
<Floor2>
<Floor1_1>
<Text1>wt3</Text1>
<Text2>t2</Text2>
</Floor1_1>
<Floor1_2>
<Text1>t1</Text1>
<Text2>t2</Text2>
</Floor1_2>
</Floor2>
<!--四级节点-->
<Floor3>
<Floor2_1>
<Floor1_1>
<Text1>wt4</Text1>
<Text2>t2</Text2>
</Floor1_1>
<Floor1_2>
<Text1>t1</Text1>
<Text2>t2</Text2>
</Floor1_2>
</Floor2_1>
<Floor2_2>
<Floor1_1>
<Text1>t1</Text1>
<Text2>t2</Text2>
</Floor1_1>
<Floor1_2>
<Text1>t1</Text1>
<Text2>t2</Text2>
</Floor1_2>
</Floor2_2>
</Floor3>
</FileRoot>