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>
*****************************************}