I would like to read an HTML document from disk, find an element by its "id", modify it and write it back out to disk. Here's what I have so far:
program project1;
{$mode objfpc}{$H+}
uses
Classes, DOM, DOM_HTML, SAX_HTML;
var
doc: THTMLDocument;
reader: THTMLReader;
converter: THTMLToDOMConverter;
stream: TFileStream;
element: TDOMElement;
begin
doc := THTMLDocument.Create;
reader := THTMLReader.Create;
converter := THTMLToDOMConverter.Create(reader, doc);
stream := TFileStream.Create('network.html', fmOpenRead);
reader.ParseStream(stream);
writeln(doc.Title); // this works
element := doc.GetElementById('BlockNumber'); // this returns nil
end.
Some sample HTML:
<html>
<head>
<title>This is a test</title>
</head>
<body>
<p id="BlockNumber">464564</p>
<p id="BlockTime">00:21</p>
</body>
</html>
As I commented in my code, I tried the GetElementById, but it returned nil. I need to know how to access an element, modify it and then write it back out. I've also considered using an XPath search, but I don't know how to do that either. Hoping someone can help me. There's not much documentation on this that I could find.