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;
{RootNodes}
procedure TForm1.Button1Click(Sender: TObject);
var
declaration: TsdDeclaration;
root: TXMLNode;
begin
ShowMessageFmt('%s, %s', [xml.VersionString, XML.EncodingString]); //1.0, UTF-8
declaration := XML.RootNodes[0] as TsdDeclaration;
ShowMessageFmt('%s, %s', [declaration.Version, declaration.Encoding]); //1.0, UTF-8
ShowMessageFmt('%s, %d', [xml.Root.Name, XML.Root.NodeCount]); //List, 3
root := XML.RootNodes[1];
ShowMessageFmt('%s, %d', [root.Name, root.NodeCount]); //List, 3
end;
procedure TForm1.Button2Click(Sender: TObject);
var
node: TXMLNode;
begin
node := XML.Root;
ShowMessage(node.WriteToString);
end;
{***************************************
<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.Button3Click(Sender: TObject);
var
node: TXMLNode;
begin
node := XML.Root[0];
ShowMessage(node.WriteToString);
end;
{***************************************
<item ID="No.1" age="11">張三</item>
*****************************************}
procedure TForm1.Button4Click(Sender: TObject);
var
node: TXMLNode;
begin
node := XML.Root[0];
ShowMessage(node.Name); //Item
ShowMessage(node.Value); //張三
ShowMessage(node.Attributes[0].Name); //ID
ShowMessage(node.Attributes[0].Value); //No.1
ShowMessage(node.Attributes[0].WriteToString); //ID="No.1"
end;
procedure TForm1.Button5Click(Sender: TObject);
var
node: TXMLNode;
begin
node := XML.Root[0];
ShowMessage(node.AttributeByName['ID'].Value); //No.1
ShowMessage(node.AttributeValueByName['ID']); //No.1
end;
procedure TForm1.Button6Click(Sender: TObject);
var
nodeList: TsdNodeList;
node: TXMLNode;
i: Integer;
begin
node := XML.Root;
ShowMessage(node.ReadString('item')); //張三
ShowMessage(node.FindNode('item').Value); //張三
ShowMessage(node.NodeByName('item').Value); //張三
//node.NodesByName(); 用法同 FindNodes
nodeList := TsdNodeList.Create(False);
node.FindNodes('item', nodeList);
for i := 0 to nodeList.Count - 1 do
begin
ShowMessage(nodeList[i].Value); //張三/李四/王五
end;
nodeList.Free;
//
for i := 0 to node.NodeCount - 1 do
begin
ShowMessage(node[i].Value); //張三/李四/王五
end;
end;
procedure TForm1.Button7Click(Sender: TObject);
var
node: TXMLNode;
begin
node := XML.Root[0];
ShowMessage(node.Parent.Name); //List
ShowMessage(node.Document.EncodingString); //UTF-8
end;
{StringTable}
procedure TForm1.Button8Click(Sender: TObject);
const
path = 'c:\temp\test.txt';
begin
XML.StringTable.SaveToFile(path);
Memo1.Lines.LoadFromFile(path, TEncoding.UTF8);
end;
{***************************************
22 1
33 1
11 1
age 3
ID 3
UTF-8 1
List 1
1.0 1
No.2 1
item 1
No.1 1
No.3 1
張三 1
item 2
王五 1
李四 1
encoding 1
version 1
*****************************************}