Delphi操作XML(二)

继续研究NativeXML:

NativeXml的方法属性:ANativeXml  := TNativeXml.Create(Self);//创建

ANativeXml.XmlFormat        := xfReadable; //格式

ANativeXml.EncodingString   := 'UTF-8';//编码

ANativeXml.ExternalEncoding := seUTF8;

ANativeXml.LoadFromFile();//从文件夹中导入XML文件

ANativeXml.SaveToFile();//XML文件保存到文件夹

节点的方法属性:

ANativeXml.Root;//XML文件的根节点,注意一切都从根开始

ANativeXml.Root.NodeByName(‘节点名’);//通过节点名获得节点

NativeXml.Root.Attributes;//节点的属性对象

NativeXml.Root.ChildContainers;//节点的子节点对象

NativeXml.Root.NodeNew();//增加新的子节点

NativeXml.Root.Name;//节点名

NativeXml.Root.Value;//节点值

七、添加节点

根节点的添加,可以在创建时直接指定根节点:

var

XmlDoc: TnativeXml;

Node:TxmlNode;

Begin

XmlDoc := TNativeXml.CreateName('Data'); //创建根节点的语法

   XmlDoc.XmlFormat := xfReadable;

... ...

添加节点有以下几种方法:

Node=xmlDoc.Root.NodeNew(‘BasicDataNo’);

Node.value:=’1’;

XmlDoc.Root.NodeNew(‘name’).value:=’sam’;

xmlDoc.Root.WriteString(‘sex’,’male);

____________________________________

更加完整的参考万一的博客,在这里引用一下:

 

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> 
*****************************************}
posted @ 2012-02-15 10:12  步履蹒跚  阅读(907)  评论(0编辑  收藏  举报