XE3随笔15:从XML中解析

SuperObject 文件包中还有一个 SuperXmlParser 单元, 可以从 XML 中解析出 ISuperObject.

SuperXmlParser 只有三个函数: XMLParseString、XMLParseStream、XMLParseFile, 分别从字符串、流、文件中解析.

遗憾的是对中文不够友好.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls; type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end; var
  Form1: TForm1; implementation {$R *.dfm} uses SuperObject, SuperXmlParser; procedure TForm1.Button1Click(Sender: TObject);
var
  jo: ISuperObject;
begin
  jo := XMLParseString('<Person><Name sex="男">张三</Name><Age>123</Age></Person>');
  ShowMessage(jo.AsString);   ShowMessage(jo['#children'].AsArray[].AsString);
  ShowMessage(jo['#children'].AsArray[].AsString);   ShowMessage(jo['#children'].AsArray[]['#children'].AsArray[].AsString); {张三}
  ShowMessage(jo['#children'].AsArray[]['#attributes.sex'].AsString);      {男}
  ShowMessage(jo['#children'].AsArray[]['#children'].AsArray[].AsString); {123}
end; //使用压缩
procedure TForm1.Button2Click(Sender: TObject);
var
  jo: ISuperObject;
begin
  jo := XMLParseString('<Person><Name sex="男">张三</Name><Age>123</Age></Person>', True);
  ShowMessage(jo.AsString);   ShowMessage(jo['Name.Name'].AsString); {张三}
  ShowMessage(jo['Name.sex'].AsString);  {男}
  ShowMessage(jo['Age'].AsString);      {123}
end; end.
上一篇:推荐两个小工具MediaInfo 和 netpersec


下一篇:简单实现高并发处理秒杀思路(redis分布式锁)