Get multiple values between tags

2

I need to get a value between the tags of a text, so I found here in the site the function:

function ExtractText(aText, OpenTag, CloseTag : String) : String;

It worked perfectly, but with only the first value found.

But if I have more equal TAGs, to add all values within those equal TAGs in a ListBox, how should I do it, since I have not found anywhere. Example:

<t1>teste1</t1>
<t2>teste2</t2>
<t1>teste3</t1>
<t1>teste4</t1>
<t3>teste5</t3>

If I get the tags t1 and / t1, I wanted to display it like this in the ListBox:

teste1
teste3
teste4
    
asked by anonymous 12.02.2015 / 01:06

1 answer

2

The function below scans a text, and checks on a loop if the current element contains the tag , if it does, it adds in the StringList. This information is obtained through the IHTMLDocument2 interface. / p>

// Incluir em Uses: ActiveX, MSHTML   
Function ParseTag(Const Texto, Tag: string): TStringList;
Var
 Doc, Elementos: OleVariant;
 I: integer;
begin
  Doc := CoHTMLDocument.Create as IHTMLDocument2;
  try      
    Result := TStringList.Create;
    Doc.write(Texto);

    for I := 0 to Doc.body.all.length - 1 do begin
      Elementos := Doc.body.all.item(I);
      if Elementos.tagName = Tag then
        Result.Add(string(Elementos.innerText));
    end;
  finally
    Doc.close;
  end;
end;

To use it call in the event OnClick() of a button:

procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.AddStrings(ParseTag('HTML aqui', 'H1'));
end;
    
12.02.2015 / 03:59