随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


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[0].AsString);
  ShowMessage(jo['#children'].AsArray[1].AsString);

  ShowMessage(jo['#children'].AsArray[0]['#children'].AsArray[0].AsString); {张三}
  ShowMessage(jo['#children'].AsArray[0]['#attributes.sex'].AsString);      {男}
  ShowMessage(jo['#children'].AsArray[1]['#children'].AsArray[0].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.

posted on 2009-10-27 16:46  万一  阅读(...)  评论(...编辑  收藏