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

NativeXml (7):添加屬性

編輯: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.AttributeAdd('ID', '1'); 
  node.AttributeAdd('age', '11'); 
  node.Value := '張三'; 
 
  with XML.Root.NodeNew('item') do 
  begin 
    AttributeAdd('ID', '2'); 
    AttributeAdd('age', '22'); 
    Value := '李四'; 
  end; 
 
  with XML.Root.NodeNew('item') do 
  begin 
    AttributeAdd('ID', '3'); 
    AttributeAdd('age', '33'); 
    Value := '王五'; 
  end; 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{*************************************** 
<?XML version="1.0" encoding="UTF-8"?> 
<List> 
 <item ID="1" age="11">張三</item> 
 <item ID="2" age="22">李四</item> 
 <item ID="3" age="33">王五</item> 
</List> 
*****************************************} 
 
procedure TForm1.Button2Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
  attr1,attr2: TsdAttribute; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  attr1 := TsdAttribute.Create(XML); 
  attr1.Name := 'ID'; 
  attr1.Value := '1'; 
  attr2 := TsdAttribute.Create(XML); 
  attr2.Name := 'age'; 
  attr2.ValueAsInteger := 11; 
  node := XML.Root.NodeNew('item'); 
  node.AttributeAdd(attr1); 
  node.AttributeAdd(attr2); 
  node.Value := '張三'; 
 
  attr1 := TsdAttribute.CreateName(XML, 'ID'); 
  attr1.Value := '2'; 
  attr2 := TsdAttribute.CreateNameValue(XML, 'age', '22'); 
  node := XML.Root.NodeNew('item'); 
  node.AttributesAdd([attr1, attr2]); 
  node.Value := '李四'; 
 
  node := XML.Root.NodeNew('item'); 
  node.AttributesAdd([TsdAttribute.CreateNameValue(xml, 'ID', '3'), TsdAttribute.CreateNameValue(XML, 'age', '33')]); 
  node.Value := '王五'; 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{結果同上} 
 
procedure TForm1.Button3Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
  attr1,attr2: TsdAttribute; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  attr1 := XML.AttrText('ID', '1'); 
  attr2 := XML.AttrInt('age', 11); 
  node := XML.Root.NodeNew('item'); 
  node.AttributesAdd([attr1, attr2]); 
  node.Value := '張三'; 
 
  node := XML.Root.NodeNew('item'); 
  node.AttributesAdd([xml.AttrText('ID', '2'), XML.AttrInt('age', 22)]); 
  node.Value := '李四'; 
 
  with XML.Root.NodeNew('item') do begin 
    AttributesAdd([xml.AttrText('ID', '3'), XML.AttrInt('age', 33)]); 
    Value := '王五'; 
  end; 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{結果同上} 
 
procedure TForm1.Button4Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  node := xml.NodeNewTextAttr('item', '張三', [xml.AttrText('ID', '1'), XML.AttrInt('age', 11)]); 
  XML.Root.NodeAdd(node); 
 
  node := xml.NodeNewTextAttr('item', '李四', [xml.AttrText('ID', '2'), XML.AttrInt('age', 22)]); 
  XML.Root.NodeAdd(node); 
 
  node := xml.NodeNewTextAttr('item', '王五', [xml.AttrText('ID', '3'), XML.AttrInt('age', 33)]); 
  XML.Root.NodeAdd(node); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{結果同上} 
 
procedure TForm1.Button5Click(Sender: TObject); 
var 
  xml: TNativeXML; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  XML.Root.NodesAdd([ 
    xml.NodeNewTextAttr('item', '張三', [xml.AttrText('ID', '1'), XML.AttrInt('age', 11)]), 
    xml.NodeNewTextAttr('item', '李四', [xml.AttrText('ID', '2'), XML.AttrInt('age', 22)]), 
    xml.NodeNewTextAttr('item', '王五', [xml.AttrText('ID', '3'), XML.AttrInt('age', 33)]) 
  ]); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{結果同上} 
 
procedure TForm1.Button6Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  node := xml.NodeNewAttr('item', [xml.AttrText('name', '張三'), XML.AttrInt('age', 11)]); 
  XML.Root.NodeAdd(node); 
 
  node := xml.NodeNewAttr('item', [xml.AttrText('name', '李四'), XML.AttrInt('age', 22)]); 
  XML.Root.NodeAdd(node); 
 
  node := xml.NodeNewAttr('item', [xml.AttrText('name', '王五'), XML.AttrInt('age', 33)]); 
  XML.Root.NodeAdd(node); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{*************************************** 
<?XML version="1.0" encoding="UTF-8"?> 
<List> 
 <item name="張三" age="11"/> 
 <item name="李四" age="22"/> 
 <item name="王五" age="33"/> 
</List> 
*****************************************} 
 
procedure TForm1.Button7Click(Sender: TObject); 
var 
  xml: TNativeXML; 
  node: TXMLNode; 
begin 
  xml := TNativeXML.CreateName('List'); 
  xml.XMLFormat := xfReadable; 
 
  XML.Root.NodesAdd([ 
    xml.NodeNewAttr('item', [xml.AttrText('name', '張三'), XML.AttrInt('age', 11)]), 
    xml.NodeNewAttr('item', [xml.AttrText('name', '李四'), XML.AttrInt('age', 22)]), 
    xml.NodeNewAttr('item', [xml.AttrText('name', '王五'), XML.AttrInt('age', 33)]) 
  ]); 
 
  Memo1.Text := XML.WriteToString; 
  XML.Free; 
end; 
{結果同上} 


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