程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> NativeXml (10):編輯

NativeXml (10):編輯

編輯:Delphi

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; 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved