如何查找一级节点、二级节点,并赋值?XML问题
例子如下
//更改
xml->LoadFromFile("C:\\Sample.xml");
//这个位置以下 怎么查找一级节点?并赋值?
_di_IXMLNode nodeupdate =xml->DocumentElement->ChildNodes->FindNode(ResultSet);
nodeupdate->AttributeNodes->operator [](1)->NodeValue=(WideString)(5);
xml->SaveToFile("Sample.xml");
[解决办法]
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Button1->Visible= false;
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
_di_IXMLDocument xml;
WideString strXmlFile = "test.xml";
if(FileExists(strXmlFile))
{
xml = LoadXMLDocument(strXmlFile);
xml->Active = true;
xml->Options = xml->Options << doNodeAutoIndent;//自动换行
_di_IXMLNode root;
root = xml->ChildNodes->FindNode("Machine")->ChildNodes->FindNode("LocalData")->ChildNodes->FindNode("paraModifyHs");
_di_IXMLNode node;
node = root->AddChild("Item");
node->Attributes[WideString("name")] = WideString("4");
node->Attributes[WideString("oldValue")] = WideString("4");
node->Attributes[WideString("newValue")] = WideString("4");
node->Attributes[WideString("modiyData")] = WideString(FormatDateTime( "YYYY/MM/DD ",Date()) + FormatDateTime( "hh:mm:ss",Time()));
xml->SaveToFile(strXmlFile);
xml->Active = false;
}
else
{
try
{
xml = NewXMLDocument();
xml->Options = xml->Options << doNodeAutoIndent;
xml->Encoding = L"GB2312";
xml->AddChild("Machine");
_di_IXMLNode root = xml->ChildNodes->FindNode("Machine");
root->Attributes[WideString("machinetype")] = WideString("GD_H500");
root->Attributes[WideString("address")] = WideString("5.4.36.211.1.1");
root->Attributes[WideString("port")] = WideString("801");
root->AddChild("LocalData");
root = root->ChildNodes->FindNode("LocalData");
root->AddChild("paraModifyHs");
root = root->ChildNodes->FindNode("paraModifyHs");
_di_IXMLNode node;
node = root->AddChild("Item");
node->Attributes[WideString("name")] = WideString("4");
node->Attributes[WideString("oldValue")] = WideString("4");
node->Attributes[WideString("newValue")] = WideString("4");
node->Attributes[WideString("modiyData")] = WideString(FormatDateTime( "YYYY/MM/DD ",Date()) + FormatDateTime( "hh:mm:ss",Time()));
xml->SaveToFile(strXmlFile);
xml->Active = false;
}
catch(...)
{
xml->Active = false;
}
}
}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
_di_IXMLDocument xml;
WideString strXmlFile = "ParaModifyHistory.xml";
if(FileExists(strXmlFile))
{
xml = LoadXMLDocument(strXmlFile);
xml->Active = true;
xml->Options = xml->Options << doNodeAutoIndent;//自动换行
_di_IXMLNode root;
root = xml->ChildNodes->FindNode("Machine")->ChildNodes->FindNode("LocalData")->ChildNodes->FindNode("paraModifyHs");
_di_IXMLNode node;
_di_IXMLNodeList list = root->ChildNodes;
String modifyDate;
SYSTEMTIME systemTime;
GetLocalTime(&systemTime);
TDateTime dt2 = SystemTimeToDateTime(systemTime);
//dt2 = StrToDateTime("2011-08-02 15:53:09");
TDateTime dt;
for(int i = 0; i<list->Count; ++i)
{
String ss = list->Nodes[i]->GetAttribute("modiyData");
dt = StrToDateTime(list->Nodes[i]->GetAttribute("modiyData"));
if(double(dt - dt2) > 30)
{
list->Delete(i);
i = -1;
}
}
xml->SaveToFile(strXmlFile);
xml->Active = false;
}
}
//---------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Button1->Visible =true;
Button1->BringToFront();
}
//---------------------------------------