zoukankan      html  css  js  c++  java
  • Delphi 中的 XMLDocument 类详解(3) 读取 xml 文件

    先虚拟一个测试文件: test.xml; 放在 c:\temp\ 下备用. <?xml version="1.0" encoding="gb2312"?> <科室名单 备注="测试"> <人员 职务="科长" 备注="正局级"> <姓名>张三</姓名> <性别>男</性别> <年龄>34</年龄> </人员> <人员 职务="付科长"> <姓名>李四</姓名> <性别>女</性别> <年龄>43</年龄> </人员> <人员> <姓名>王五</姓名> <性别>女</性别> <年龄>25</年龄> </人员> <人员> <姓名>孙六</姓名> <性别>男</性别> <年龄>52</年龄> </人员> <辅助人员></辅助人员> </科室名单>
    unit Unit1;
    
    interface
    
    uses
      Classes, Controls, Forms, StdCtrls, XMLDoc, xmldom, XMLIntf, msxmldom;
    
    type
      TForm1 = class(TForm)
        XMLDocument1: TXMLDocument;
        Memo1: TMemo;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    //载入方法1: LoadFromFile
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      XMLDocument1.LoadFromFile('c:\temp\test.xml');
    
      Memo1.Lines := XMLDocument1.XML; {查看}
    end;
    
    
    //载入方法2: 指定 FileName, 然后激活
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      XMLDocument1.FileName := 'c:\temp\test.xml';
      XMLDocument1.Active := True;     {激活}
    
      Memo1.Lines := XMLDocument1.XML; {查看}
    end;
    
    
    //载入方法3: 指定 LoadFromStream
    procedure TForm1.Button3Click(Sender: TObject);
    var
      ms: TMemoryStream;
    begin
      ms := TMemoryStream.Create;
      ms.LoadFromFile('c:\temp\test.xml');
      XMLDocument1.LoadFromStream(ms);
      ms.Free;
    
      Memo1.Lines := XMLDocument1.XML; {查看}
    end;
    
    
    //可以用 LoadFromFile 或指定 FileName 的方法, 访问网上的 xml
    procedure TForm1.Button4Click(Sender: TObject);
    begin
      XMLDocument1.LoadFromFile('http://www.google.com/ig/skins/jr.xml');
    
      Memo1.Lines := XMLDocument1.XML; {查看}
    end;
    
    end.
    
    如果需要用浏览器查看 xml, 需要一个 api 函数: ShellAPI.ShellExecute, 所以先 uses ShellAPI; 然后: ShellExecute(Handle, 'open', 'c:\temp\test.xml', nil, nil, SW_NORMAL);
  • 相关阅读:
    第37天新版动画系统和有限状态机
    第36天旧版动画系统
    第35天2D游戏相关
    第34天协同程序和异步加载
    第33天力、射线检测、球形检测和延迟函数
    第32天Line渲染器,物理系统和力
    第31天Camera组件和灯光组件
    第29天动态加载、对象池
    第28天3D数学
    第27天3D数学
  • 原文地址:https://www.cnblogs.com/del/p/1024269.html
Copyright © 2011-2022 走看看