我如何用npapi阅读每个参数

时间:2015-05-05 11:26:30

标签: delphi npapi

我在我的Delphi项目中使用npapi框架工作,我能用这个当前代码读取params

procedure TDemoPluginForm.btTestClick(Sender: TObject);
var
  obj: IBrowserObject;
  res: TStringList;
  items, n: IBrowserObject;
  i: integer;
begin
  res:=TStringList.Create;
  try
    res.Add('Plugin element details:');
    // Get object of plugin element
    obj:=Plugin.GetPluginObject;
    // Get element property
    res.Add('id=' + string(obj['id']));
    // Get child elements
    res.Add('Child nodes:');
    items:=obj.GetObject('childNodes');
    for i:=0 to items['length'] - 1 do begin
      n:=VarAsObject(items.Invoke('item', [i]));
      if CompareText(n['tagName'], 'param') = 0 then
        res.Add(Format('Tag: %s; Name: %s; Value: %s',
                      [string(n['tagName']),
                       string(n['name']),
                       string(n['value'])
                       ]));
    end;
    MessageBox(Self.Handle, PChar(res.Text), PChar('Delphi Plugin'), MB_ICONINFORMATION);
  finally
    res.Free;
  end;
end;

我怎么能用它的参考阅读每个参数。

如果我有像这样的参数

,那就更具体了
  <param name = "delphi" value = "student" />

如果param = delphi,那么我怎么能在delphi中读取somestring:=它的值?

1 个答案:

答案 0 :(得分:1)

您可以轻松获得答案,可以使用System.SysUtils.SameText来实现目标:

procedure TDemoPluginForm.btTestClick(Sender: TObject);
var
  obj: IBrowserObject;
  res: TStringList;
  items, n: IBrowserObject;
  i: integer;
  value : String;

begin
  res:=TStringList.Create;
  try
    res.Add('Plugin element details:');
    // Get object of plugin element
    obj:=Plugin.GetPluginObject;
    // Get element property
    res.Add('id=' + string(obj['id']));
    // Get child elements
    res.Add('Child nodes:');
    items:=obj.GetObject('childNodes');
    for i:=0 to items['length'] - 1 do begin
      n:=VarAsObject(items.Invoke('item', [i]));
      if SameText(n['tagName'], 'param') then
       begin 
        res.Add(Format('Tag: %s; Name: %s; Value: %s',
                      [string(n['tagName']),
                       string(n['name']),
                       string(n['value'])
                       ]));
        if SameText(n['name'], 'delphi') then
         value := String(n['value']);
      end;
    end;
    MessageBox(Self.Handle, PChar(res.Text), PChar('Delphi Plugin'), MB_ICONINFORMATION);
  finally
    res.Free;
  end;
end;