JSON 之 SuperObject(14): 從 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[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.
相關文章
相關標籤/搜索