Delphi7的HtmlParser使用方法

2018-06-17 18:57:32来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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;

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:[示例] Firemonkey 画出 1 点像素的线

下一篇:延时程序