#include "tinyxml.h"
TiXmlDocument* myDocument;//文档节点
AnsiString str = ExtractFilePath(Application->ExeName)+"SQLServer.xml";//路径
TiXmlElement* ip;//节点IP地址
//读出文件,显示到Memo1中
void __fastcall TForm1::Button2Click(TObject *Sender)
{
myDocument = new TiXmlDocument();
bool a=myDocument->LoadFile(str.c_str());
if(a)
{
TiXmlElement * rootElement = myDocument->RootElement(); //根节点Class
TiXmlElement * Connecting = rootElement->FirstChildElement(); //全部子节点Students
//TiXmlElement* studentElement = studentsElement->FirstChildElement(); //元素Student
while ( Connecting )//students中的全部student
{
ip = Connecting->FirstChildElement();//获得student的phone元素
Memo1->Lines->Add(AnsiString("IP") + " : " + ip->GetText());
TiXmlElement* db = ip->NextSiblingElement();
Memo1->Lines->Add(AnsiString("数据库名:") + " : " + db->GetText() );
TiXmlElement* uid = db->NextSiblingElement();
Memo1->Lines->Add(AnsiString("用户名") + " : " + uid->GetText() );
TiXmlElement* pw = uid->NextSiblingElement();
Memo1->Lines->Add(AnsiString("密码") + " : " + pw->GetText() );
Connecting = Connecting->NextSiblingElement();
}
}
}
//---------------------------------------------------------------------------
//更新节点文本
void __fastcall TForm1::Button3Click(TObject *Sender)
{
TiXmlText newText("123.21.175.90");
TiXmlNode* val_node = (TiXmlNode* )(ip->FirstChild());
if (val_node == NULL)
{
return ;
}
ip->ReplaceChild(val_node, newText);
myDocument->SaveFile(str.c_str());//保存到文件
}
//---------------------------------------------------------------------------
XML文件内容: