zoukankan      html  css  js  c++  java
  • Delphi操作XML(四)

    以下引自万一:

    关于NativeXML的编辑操作:

    SourceURL:http://www.cnblogs.com/del/archive/2011/03/25/1995863.htmlXml (10)锛氱紪杈� - 涓囦竴 - 鍗氬鍥�
    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;


  • 相关阅读:
    Java实现 蓝桥杯VIP 算法训练 黑色星期五
    Java实现 蓝桥杯VIP 算法训练 比赛安排
    Java实现 蓝桥杯VIP 算法训练 比赛安排
    Java实现 蓝桥杯VIP 算法训练 斜率计算
    Java实现 蓝桥杯VIP 算法训练 斜率计算
    Java实现 蓝桥杯VIP 算法训练 整数平均值
    Java实现 蓝桥杯VIP 算法训练 整数平均值
    控件动态产生器(使用RegisterClasses提前进行注册)
    Delphi编写自定义控件以及接口的使用(做了一个TpgDbEdit)
    Log4delphi使用心得
  • 原文地址:https://www.cnblogs.com/acuier/p/2352189.html
Copyright © 2011-2022 走看看