NativeXml (7):添加属性



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; 

{结果同上}



你可能感兴趣的:(native)