程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> NativeXml (9):讀取

NativeXml (9):讀取

編輯:Delphi

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


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved