NativeXml (9):读取


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

你可能感兴趣的:(NativeXml (9):读取)