以下引自万一:
关于NativeXML的编辑操作:
uses NativeXml; var xml: TNativeXml; const xmlStr = '<?xml version="1.0" encoding="UTF-8"?>' + '<List>' + '<item ID="No.1" age="11">寮犱笁</item>' + '<item ID="No.2" age="22">鏉庡洓</item>' + '<item ID="No.3" age="33">鐜嬩簲</item>' + '</List>'; procedure TForm1.FormCreate(Sender: TObject); begin xml := TNativeXml.Create(Self); xml.XmlFormat := xfReadable; xml.ReadFromString(xmlStr); {!} Memo1.Text := xml.WriteToString; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item ID="No.1" age="11">寮犱笁</item> <item ID="No.2" age="22">鏉庡洓</item> <item ID="No.3" age="33">鐜嬩簲</item> </List> *****************************************} {鎻掑叆銆佸垹闄 procedure TForm1.Button2Click(Sender: TObject); begin xml.ReadFromString(xmlStr); xml.Root.NodeInsert(1, xml.NodeNewText('item', '璧靛叚')); xml.Root.NodeDelete(xml.Root.NodeCount - 1); Memo1.Text := xml.WriteToString; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item ID="No.1" age="11">寮犱笁</item> <item>璧靛叚</item> <item ID="No.2" age="22">鏉庡洓</item> </List> *****************************************} {绉婚櫎} procedure TForm1.Button3Click(Sender: TObject); begin xml.ReadFromString(xmlStr); xml.Root.NodeRemove(xml.Root[0]); Memo1.Text := xml.WriteToString; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item ID="No.2" age="22">鏉庡洓</item> <item ID="No.3" age="33">鐜嬩簲</item> </List> *****************************************} {浜ゆ崲} procedure TForm1.Button4Click(Sender: TObject); begin xml.ReadFromString(xmlStr); xml.Root.NodeExchange(0, 1); Memo1.Text := xml.WriteToString; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item ID="No.2" age="22">鏉庡洓</item> <item ID="No.1" age="11">寮犱笁</item> <item ID="No.3" age="33">鐜嬩簲</item> </List> *****************************************} {鎻愬彇} procedure TForm1.Button5Click(Sender: TObject); var node: TXmlNode; begin xml.ReadFromString(xmlStr); node := xml.Root.NodeExtract(xml.Root[1]); Memo1.Text := node.WriteToString; Memo1.Lines.Add('---------------------------------------'); Memo1.Lines.Add(xml.WriteToString); end; {*************************************** <item ID="No.2" age="22">鏉庡洓</item> --------------------------------------- <?xml version="1.0" encoding="UTF-8"?> <List> <item ID="No.1" age="11">寮犱笁</item> <item ID="No.3" age="33">鐜嬩簲</item> </List> *****************************************} {鑾峰彇浣嶇疆} procedure TForm1.Button6Click(Sender: TObject); var n1,n2: Integer; begin xml.ReadFromString(xmlStr); n1 := xml.Root.NodeIndexOf(xml.Root[xml.Root.NodeCount-1]); ShowMessage(IntToStr(n1)); //2 n2 := xml.Root.NodeIndexOf(xml.Root[xml.Root.NodeCount]); ShowMessage(IntToStr(n2)); //-1 end; procedure TForm1.Button7Click(Sender: TObject); var node: TXmlNode; begin xml.ReadFromString(xmlStr); node := xml.Root.NodeFindOrCreate('item'); ShowMessage(node.WriteToString); //<item ID="No.1" age="11">寮犱笁</item> node := xml.Root.NodeFindOrCreate('寮犱笁'); ShowMessage(node.WriteToString); //<寮犱笁/> Memo1.Text := xml.WriteToString; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item ID="No.1" age="11">寮犱笁</item> <item ID="No.2" age="22">鏉庡洓</item> <item ID="No.3" age="33">鐜嬩簲</item> <寮犱笁/> </List> *****************************************} procedure TForm1.Button8Click(Sender: TObject); var node: TXmlNode; begin xml.ReadFromString(xmlStr); node := xml.Root[0]; node.Clear; Memo1.Text := xml.WriteToString; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item/> <item ID="No.2" age="22">鏉庡洓</item> <item ID="No.3" age="33">鐜嬩簲</item> </List> *****************************************} xml.Root.DeleteEmptyNodes; Memo1.Lines.Add('---------------------------------------'); Memo1.Lines.Add(xml.WriteToString); {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item ID="No.2" age="22">鏉庡洓</item> <item ID="No.3" age="33">鐜嬩簲</item> </List> *****************************************} xml.Root.Clear; Memo1.Lines.Add('---------------------------------------'); Memo1.Lines.Add(xml.WriteToString); {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List/> *****************************************} xml.Clear; Memo1.Lines.Add('---------------------------------------'); Memo1.Lines.Add(xml.WriteToString); {*************************************** *****************************************} end;