xxxxxxxxxx
uses NativeXml;
var xml: TNativeXml;
const
xmlStr = '<?xml version="1.0" encoding="UTF-8"?>' +
'<List>' +
'<item ID="No.1" age="11">张三</item>' +
'<item ID="No.2" age="22">李四</item>' +
'<item ID="No.3" age="33">王五</item>' +
'</List>';
procedure TForm1.FormCreate(Sender: TObject);
begin
xml := TNativeXml.Create(Self);
xml.XmlFormat := xfReadable;
xml.ReadFromString(xmlStr); {!}
Memo1.Text := xml.WriteToString;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
<item ID="No.1" age="11">张三</item>
<item ID="No.2" age="22">李四</item>
<item ID="No.3" age="33">王五</item>
</List>
*****************************************}
{插入、删除}
procedure TForm1.Button2Click(Sender: TObject);
begin
xml.ReadFromString(xmlStr);
xml.Root.NodeInsert(1, xml.NodeNewText('item', '赵六'));
xml.Root.NodeDelete(xml.Root.NodeCount - 1);
Memo1.Text := xml.WriteToString;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
<item ID="No.1" age="11">张三</item>
<item>赵六</item>
<item ID="No.2" age="22">李四</item>
</List>
*****************************************}
{移除}
procedure TForm1.Button3Click(Sender: TObject);
begin
xml.ReadFromString(xmlStr);
xml.Root.NodeRemove(xml.Root[0]);
Memo1.Text := xml.WriteToString;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
<item ID="No.2" age="22">李四</item>
<item ID="No.3" age="33">王五</item>
</List>
*****************************************}
{交换}
procedure TForm1.Button4Click(Sender: TObject);
begin
xml.ReadFromString(xmlStr);
xml.Root.NodeExchange(0, 1);
Memo1.Text := xml.WriteToString;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
<item ID="No.2" age="22">李四</item>
<item ID="No.1" age="11">张三</item>
<item ID="No.3" age="33">王五</item>
</List>
*****************************************}
{提取}
procedure TForm1.Button5Click(Sender: TObject);
var
node: TXmlNode;
begin
xml.ReadFromString(xmlStr);
node := xml.Root.NodeExtract(xml.Root[1]);
Memo1.Text := node.WriteToString;
Memo1.Lines.Add('---------------------------------------');
Memo1.Lines.Add(xml.WriteToString);
end;
{***************************************
<item ID="No.2" age="22">李四</item>
---------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<List>
<item ID="No.1" age="11">张三</item>
<item ID="No.3" age="33">王五</item>
</List>
*****************************************}
{获取位置}
procedure TForm1.Button6Click(Sender: TObject);
var
n1,n2: Integer;
begin
xml.ReadFromString(xmlStr);
n1 := xml.Root.NodeIndexOf(xml.Root[xml.Root.NodeCount-1]);
ShowMessage(IntToStr(n1)); //2
n2 := xml.Root.NodeIndexOf(xml.Root[xml.Root.NodeCount]);
ShowMessage(IntToStr(n2)); //-1
end;
procedure TForm1.Button7Click(Sender: TObject);
var
node: TXmlNode;
begin
xml.ReadFromString(xmlStr);
node := xml.Root.NodeFindOrCreate('item');
ShowMessage(node.WriteToString); //<item ID="No.1" age="11">张三</item>
node := xml.Root.NodeFindOrCreate('张三');
ShowMessage(node.WriteToString); //<张三/>
Memo1.Text := xml.WriteToString;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
<item ID="No.1" age="11">张三</item>
<item ID="No.2" age="22">李四</item>
<item ID="No.3" age="33">王五</item>
<张三/>
</List>
*****************************************}
procedure TForm1.Button8Click(Sender: TObject);
var
node: TXmlNode;
begin
xml.ReadFromString(xmlStr);
node := xml.Root[0];
node.Clear;
Memo1.Text := xml.WriteToString;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
<item/>
<item ID="No.2" age="22">李四</item>
<item ID="No.3" age="33">王五</item>
</List>
*****************************************}
xml.Root.DeleteEmptyNodes;
Memo1.Lines.Add('---------------------------------------');
Memo1.Lines.Add(xml.WriteToString);
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
<item ID="No.2" age="22">李四</item>
<item ID="No.3" age="33">王五</item>
</List>
*****************************************}
xml.Root.Clear;
Memo1.Lines.Add('---------------------------------------');
Memo1.Lines.Add(xml.WriteToString);
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List/>
*****************************************}
xml.Clear;
Memo1.Lines.Add('---------------------------------------');
Memo1.Lines.Add(xml.WriteToString);
{***************************************
*****************************************}
end;