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

NativeXml (6):添加節點

編輯:Delphi

uses NativeXML; 
 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  // 
  node := XML.Root.NodeNew('item'); 
  node.Value := '張三'; 
  // 
  with XML.Root.NodeNew('item') do 
  begin 
    Value := '李四'; 
  end; 
  // 
  XML.Root.NodeNew('item').Value := '王五'; 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{*************************************** 
<?XML version="1.0" encoding="UTF-8"?> 
<List> 
 <item>張三</item> 
 <item>李四</item> 
 <item>王五</item> 
</List> 
*****************************************} 
 
procedure TForm1.Button2Click(Sender: TObject); 
var 
  xml: TNativeXML; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  XML.Root.WriteString('N1', '張三'); 
  XML.Root.WriteString('N2', '李四'); 
  XML.Root.WriteString('N2', '王五'); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{*************************************** 
<?XML version="1.0" encoding="UTF-8"?> 
<List> 
 <N1>張三</N1> 
 <N2>王五</N2> 
</List> 
*****************************************} 
 
procedure TForm1.Button3Click(Sender: TObject); 
var 
  xml: TNativeXML; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  XML.Root.NodeNew('item').WriteString('name', '張三'); 
  XML.Root.NodeNew('item').WriteString('name', '李四'); 
  XML.Root.NodeNew('item').WriteString('name', '王五'); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{*************************************** 
<?XML version="1.0" encoding="UTF-8"?> 
<List> 
 <item> 
 <name>張三</name> 
 </item> 
 <item> 
 <name>李四</name> 
 </item> 
 <item> 
 <name>王五</name> 
 </item> 
</List> 
*****************************************} 
 
procedure TForm1.Button4Click(Sender: TObject); 
var 
  xml: TNativeXML; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  with XML.Root.NodeNew('item') do 
  begin 
    WriteString('name', '張三'); 
    WriteInteger('age', 11); 
  end; 
 
  with XML.Root.NodeNew('item') do 
  begin 
    WriteString('name', '李四'); 
    WriteInteger('age', 22);    //WriteFloat()、WriteDateTime()、WriteHex()、WriteBool()... 
  end; 
 
  with XML.Root.NodeNew('item') do 
  begin 
    WriteString('name', '王五'); 
    WriteInteger('age', 33); 
  end; 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{*************************************** 
<?XML version="1.0" encoding="UTF-8"?> 
<List> 
 <item> 
 <name>張三</name> 
 <age>11</age> 
 </item> 
 <item> 
 <name>李四</name> 
 <age>22</age> 
 </item> 
 <item> 
 <name>王五</name> 
 <age>33</age> 
 </item> 
</List> 
*****************************************} 
 
procedure TForm1.Button5Click(Sender: TObject); 
var 
  xml: TNativeXML; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  with XML.Root.NodeNewAtIndex(0, 'item') do 
  begin 
    WriteString('name', '張三'); 
    WriteInteger('age', 11); 
  end; 
 
  with XML.Root.NodeNewAtIndex(0, 'item') do 
  begin 
    WriteString('name', '李四'); 
    WriteInteger('age', 22); 
  end; 
 
  with XML.Root.NodeNewAtIndex(0, 'item') do 
  begin 
    WriteString('name', '王五'); 
    WriteInteger('age', 33); 
  end; 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{*************************************** 
<?XML version="1.0" encoding="UTF-8"?> 
<List> 
 <item> 
 <name>王五</name> 
 <age>33</age> 
 </item> 
 <item> 
 <name>李四</name> 
 <age>22</age> 
 </item> 
 <item> 
 <name>張三</name> 
 <age>11</age> 
 </item> 
</List> 
*****************************************} 
 
procedure TForm1.Button6Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  node := XML.NodeNew('item'); 
  node.WriteString('name', '張三'); 
  node.WriteInteger('age', 11); 
  XML.Root.NodeAdd(node); 
 
  XML.NodeNewEx('item', node);      //NodeNewEx 
  node.WriteString('name', '李四'); 
  node.WriteInteger('age', 22); 
  XML.Root.NodeAdd(node); 
 
  node := XML.NodeNew('item'); 
  node.WriteString('name', '王五'); 
  node.WriteInteger('age', 33); 
  XML.Root.NodeAdd(node); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{*************************************** 
<?XML version="1.0" encoding="UTF-8"?> 
<List> 
 <item> 
 <name>張三</name> 
 <age>11</age> 
 </item> 
 <item> 
 <name>李四</name> 
 <age>22</age> 
 </item> 
 <item> 
 <name>王五</name> 
 <age>33</age> 
 </item> 
</List> 
*****************************************} 
 
procedure TForm1.Button7Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  node := xml.NodeNew('item', [xml.NodeNewText('name', '張三'), XML.NodeNewInt('age', 11)]); 
  XML.Root.NodeAdd(node); 
 
  node := xml.NodeNew('item', [xml.NodeNewText('name', '李四'), XML.NodeNewInt('age', 22)]); 
  XML.Root.NodeAdd(node); 
 
  node := xml.NodeNew('item', [xml.NodeNewText('name', '王五'), XML.NodeNewInt('age', 33)]); 
  XML.Root.NodeAdd(node); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{結果同上} 
 
procedure TForm1.Button8Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  XML.Root.NodesAdd([ 
    xml.NodeNew('item', [xml.NodeNewText('name', '張三'), XML.NodeNewInt('age', 11)]), 
    xml.NodeNew('item', [xml.NodeNewText('name', '李四'), XML.NodeNewInt('age', 22)]), 
    xml.NodeNew('item', [xml.NodeNewText('name', '王五'), XML.NodeNewInt('age', 33)]) 
  ]); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{結果同上} 
 
procedure TForm1.Button9Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  node := xml.NodeNew('A', [xml.NodeNew('B', [xml.NodeNew('C', [XML.NodeNewText('D', 'E')])])]); 
  XML.Root.NodeAdd(node); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{*************************************** 
<?XML version="1.0" encoding="UTF-8"?> 
<List> 
 <A> 
 <B> 
  <C> 
  <D>E</D> 
  </C> 
 </B> 
 </A> 
</List> 
*****************************************} 


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