SAX_HTML GetElementById not work lazarus / 62-General ---------------------------------------------------------------------- wytwyt02 · 2020-06-06 05:44 for example, I have html following: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i                 Document       
               

test paragraph

       
and parse with pascal: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  doc: THTMLDocument;  PElement: TDOMElement;begin  ReadHTMLFile(doc, GetCurrentDir + '\index.html');  PElement := doc.GetElementById('test'); // PElement  is always nilend;  But the PElement is always nil, I cannot get element by id, But if I using doc.GetElementsByTagName will works: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  doc: THTMLDocument;  InnerText: DOMString;  PElement: TDOMNodeList;begin  ReadHTMLFile(doc, GetCurrentDir + '\index.html');  PElement := doc.GetElementsByTagName('p');  InnerText := PElement[0].TextContent;  DebugLn(InnerText); // It print 'test paragraph' to console, it mean worksend; ---------------------------------------------------------------------- wytwyt02 · 2020-06-06 08:03 I can print the attribute name and value with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  doc: THTMLDocument;  PElement: TDOMNodeList;  arrtibutes: TDOMNamedNodeMap;  i: Integer;begin  ReadHTMLFile(doc, GetCurrentDir + '\index.html');  PElement := doc.GetElementsByTagName('p');  arrtibutes := PElement[0].Attributes;  for i := 0 to Pred(arrtibutes.Length) do  begin    DebugLn(arrtibutes[i].NodeName+ ':' + arrtibutes[i].NodeValue); // will print id:test  end;end; But I still do not know how to GetElementById ---------------------------------------------------------------------- dsiders · 2020-06-06 09:50 Quote from: wytwyt02 on June 06, 2020, 10:03:20 amI can print the attribute name and value with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  doc: THTMLDocument;  PElement: TDOMNodeList;  arrtibutes: TDOMNamedNodeMap;  i: Integer;begin  ReadHTMLFile(doc, GetCurrentDir + '\index.html');  PElement := doc.GetElementsByTagName('p');  arrtibutes := PElement[0].Attributes;  for i := 0 to Pred(arrtibutes.Length) do  begin    DebugLn(arrtibutes[i].NodeName+ ':' + arrtibutes[i].NodeValue); // will print id:test  end;end; But I still do not know how to GetElementById Short answer: You can't. Longer answer: IDs are maintained in a THashList that is never assigned in the ancestor (TDomDocument) or any of it's descendants. GetElementByID checks the hash list, and when not assigned always returns Nil. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TDOMDocument.GetElementById(const ElementID: DOMString): TDOMElement;begin  Result := nil;  if Assigned(FIDList) then    Result := TDOMElement(FIDList.Get(DOMPChar(ElementID), Length(ElementID)));end; ---------------------------------------------------------------------- Jurassic Pork · 2020-06-06 12:20 hello, you can also use XPath  to get html element by id with the package internettools  : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program xPathTest; uses Classes, SysUtils, simpleinternet;var   extFile : TStringList;    begin   extFile := TStringList.Create();   extFile.LoadFromFile(GetCurrentDir + '\index.html');   writeln(process(extFile.text,'//p[@id="test"]').toString);     extFile.Free;     end.  Friendly J.P ---------------------------------------------------------------------- wytwyt02 · 2020-06-06 13:09 Quote from: Jurassic Pork on June 06, 2020, 02:20:16 pmhello, you can also use XPath  to get html element by id with the package internettools  : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program xPathTest; uses Classes, SysUtils, simpleinternet;var   extFile : TStringList;    begin   extFile := TStringList.Create();   extFile.LoadFromFile(GetCurrentDir + '\index.html');   writeln(process(extFile.text,'//p[@id="test"]').toString);     extFile.Free;     end.  Friendly J.P I tried internettools, It works fine with Read document, but works difficult with Write (like find a dom and change it's properties). ---------------------------------------------------------------------- wytwyt02 · 2020-06-06 13:14 Quote from: dsiders on June 06, 2020, 11:50:59 amQuote from: wytwyt02 on June 06, 2020, 10:03:20 amI can print the attribute name and value with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  doc: THTMLDocument;  PElement: TDOMNodeList;  arrtibutes: TDOMNamedNodeMap;  i: Integer;begin  ReadHTMLFile(doc, GetCurrentDir + '\index.html');  PElement := doc.GetElementsByTagName('p');  arrtibutes := PElement[0].Attributes;  for i := 0 to Pred(arrtibutes.Length) do  begin    DebugLn(arrtibutes[i].NodeName+ ':' + arrtibutes[i].NodeValue); // will print id:test  end;end; But I still do not know how to GetElementById Short answer: You can't. Longer answer: IDs are maintained in a THashList that is never assigned in the ancestor (TDomDocument) or any of it's descendants. GetElementByID checks the hash list, and when not assigned always returns Nil. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TDOMDocument.GetElementById(const ElementID: DOMString): TDOMElement;begin  Result := nil;  if Assigned(FIDList) then    Result := TDOMElement(FIDList.Get(DOMPChar(ElementID), Length(ElementID)));end;  oh my god , I do not wanna to use Regex, and do not like internettools with strange api, this features is really necessary for a web site crawler. ---------------------------------------------------------------------- dsiders · 2020-06-06 13:44 Quote from: wytwyt02 on June 06, 2020, 03:14:09 pmQuote from: dsiders on June 06, 2020, 11:50:59 amQuote from: wytwyt02 on June 06, 2020, 10:03:20 amI can print the attribute name and value with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  doc: THTMLDocument;  PElement: TDOMNodeList;  arrtibutes: TDOMNamedNodeMap;  i: Integer;begin  ReadHTMLFile(doc, GetCurrentDir + '\index.html');  PElement := doc.GetElementsByTagName('p');  arrtibutes := PElement[0].Attributes;  for i := 0 to Pred(arrtibutes.Length) do  begin    DebugLn(arrtibutes[i].NodeName+ ':' + arrtibutes[i].NodeValue); // will print id:test  end;end; But I still do not know how to GetElementById Short answer: You can't. Longer answer: IDs are maintained in a THashList that is never assigned in the ancestor (TDomDocument) or any of it's descendants. GetElementByID checks the hash list, and when not assigned always returns Nil. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TDOMDocument.GetElementById(const ElementID: DOMString): TDOMElement;begin  Result := nil;  if Assigned(FIDList) then    Result := TDOMElement(FIDList.Get(DOMPChar(ElementID), Length(ElementID)));end;  oh my god , I do not wanna to use Regex, and do not like internettools with strange api, this features is really necessary for a web site crawler. Your example specifically used THTMLDocument from fpc. You could use TDOMDocument from the Lazarus LazUtils package. It implements GetELementByID, but requires well-formed XML content which HTML5 (also in your example) is not. Choices, choices.... ---------------------------------------------------------------------- wytwyt02 · 2020-06-07 02:31 Quote from: dsiders on June 06, 2020, 03:44:18 pmQuote from: wytwyt02 on June 06, 2020, 03:14:09 pmQuote from: dsiders on June 06, 2020, 11:50:59 amQuote from: wytwyt02 on June 06, 2020, 10:03:20 amI can print the attribute name and value with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  doc: THTMLDocument;  PElement: TDOMNodeList;  arrtibutes: TDOMNamedNodeMap;  i: Integer;begin  ReadHTMLFile(doc, GetCurrentDir + '\index.html');  PElement := doc.GetElementsByTagName('p');  arrtibutes := PElement[0].Attributes;  for i := 0 to Pred(arrtibutes.Length) do  begin    DebugLn(arrtibutes[i].NodeName+ ':' + arrtibutes[i].NodeValue); // will print id:test  end;end; But I still do not know how to GetElementById Short answer: You can't. Longer answer: IDs are maintained in a THashList that is never assigned in the ancestor (TDomDocument) or any of it's descendants. GetElementByID checks the hash list, and when not assigned always returns Nil. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TDOMDocument.GetElementById(const ElementID: DOMString): TDOMElement;begin  Result := nil;  if Assigned(FIDList) then    Result := TDOMElement(FIDList.Get(DOMPChar(ElementID), Length(ElementID)));end;  oh my god , I do not wanna to use Regex, and do not like internettools with strange api, this features is really necessary for a web site crawler. Your example specifically used THTMLDocument from fpc. You could use TDOMDocument from the Lazarus LazUtils package. It implements GetELementByID, but requires well-formed XML content which HTML5 (also in your example) is not. Choices, choices.... What if the html structure not well-formed when use TDOMDocument ? ---------------------------------------------------------------------- dsiders · 2020-06-07 03:14 Quote from: wytwyt02 on June 07, 2020, 04:31:18 amWhat if the html structure not well-formed when use TDOMDocument ? Well, I'm assuming you'll actually use TXMLDcument, and call ReadXMLFile or TXMLReader to load the file. In that case, an EXMLReadError exception is raised. There is a lot of good info on the Wiki at: https://wiki.lazarus.freepascal.org/XML_Tutorial