TOP

Delphi7的HtmlParser使用方法
2017-10-10 12:04:17 】 浏览:8839
Tags:Delphi7 HtmlParser 使用方法

uses
  HtmlParser

procedure TForm4.Button1Click(Sender: TObject);
var FNodes:IHtmlElement;
   aString:string;
begin
  FNodes := parserHtml(Memo1.Text);
 aString:= FNodes.Children[2].Children[1].Children[5].Children[0].InnerText;
end;
//判断某子结点是否存在,增加代码的可靠性
//if NodeIsExist(FNodes,
//'Children[2].Children[3].Children[11].Children[3].Children[1].Children[1].Children[1].Children[0]'
//)  then
//Memo3.Lines.Add(
//FNodes.Children[2].Children[3].Children[11].Children[3].Children[1].Children[1].Children[1].Children[0].InnerText
// );
  function NodeIsExist(Root:IHtmlElement;Childrens:string):Boolean ;
  var aStringlist:tStringlist;   i,aIndex:integer;
      aNode:IHtmlElement;   aStr:string;
  begin
    Result:= False;
    aStringlist:=TStringList.Create;
  aStringlist.Delimiter := '.';
  aStringlist.DelimitedText := Childrens;

  aNode:=Root;
  for i := 0 to aStringlist.Count-1 do  begin
    aStr:=Copy(aStringlist[i],10,Length(aStringlist[i])-10);
    aIndex:= StrToIntDef(aStr,0);
   if aIndex>=  aNode.ChildrenCount  then  begin
    aStringlist.Free;
    Exit;
   end;
     aNode:=aNode.Children[aIndex];
  end;
   aStringlist.Free;
   Result:= true;
  end;

      HtmlParser例子

 //NodeIsExist的优化版,更高速,只处理最大结点0~99
 function NodeIsExist(Root:IHtmlElement;Childrens:string):Boolean ;
  VAR i:integer;
   aNode:IHtmlElement;
  begin
     Result:= False;
     aNode:=Root;
     for i:=1 to Length(Childrens) do    begin //aString[1]从1开始,而不是从零开始
       if Childrens[i]=#93 then begin  //  ']'
           if Childrens[i-2]=#91 then    begin    //  '['
           if StrToIntDef(Copy(Childrens,i-1,1),0)>=  aNode.ChildrenCount  then   Exit;  //一位数
           aNode:=aNode.Children[StrToIntDef(Copy(Childrens,i-1,1),0)];
           end;
           if Childrens[i-3]=#91 then    begin //  '['
           if StrToIntDef(Copy(Childrens,i-2,2),0)>=  aNode.ChildrenCount  then   Exit;  //二位数
           aNode:=aNode.Children[StrToIntDef(Copy(Childrens,i-2,2),0)];
           end;
       end;

     end;
    Result:= true;
  end;

 


Delphi7的HtmlParser使用方法 https://www.cppentry.com/bencandy.php?fid=86&id=130029

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[示例] Firemonkey OnTouch 多点.. 下一篇[修正] Firemonkey 中英文混排折..