read a xml with html tags...
lazarus / 21-General
----------------------------------------------------------------------
Espectr0 · 2022-08-26 11:36
Hola,
I have a problem reading an XML file in which part of the text
includes the
tags but it doesn't read them.
i use:
...
XmlDoc: TXMLDocument;
Node: TDOMNode;
...
ReadXMLFile(XmlDoc, FileName);
...
Text := Node.TextContent;
...
I need to add any parameters/flag?
Thanks!
----------------------------------------------------------------------
dje · 2022-08-26 12:04
Why would a XML tag be part of TextContent?
----------------------------------------------------------------------
Espectr0 · 2022-08-26 12:48
Quote from: dje on August 26, 2022, 02:04:41 pmWhy would a XML tag be
part of TextContent?
It is part of a subtitle format
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
- lost in America.
- [man] He’s
coming!
----------------------------------------------------------------------
MarkMLl · 2022-08-26 13:14
I'm by no means an expert at this but please could we have a reality
check: is your source document well-formed XML, or are you actually
trying to read HTML into an XML component?
MarkMLl
----------------------------------------------------------------------
Espectr0 · 2022-08-26 13:32
Quote from: MarkMLl on August 26, 2022, 03:14:13 pmI'm by no means an
expert at this but please could we have a reality check: is your
source document well-formed XML, or are you actually trying to read
HTML into an XML component?
MarkMLl
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 Blockedltiplier="999 1000" ttp:dropMode="nonDrop">
He’s a country boy from China
- lost in America.
- [man] He’s
coming!
Watch out!
Oh, watch out!
[indistinct
shouting]
[yells]
Searching for a master
who faces forces
beyond even
his legendary powers.
This is Los Angeles
where the California dream can
become
a nightmare in the
blink of an eye.
I understand that it is well formed, and I read everything correctly,
my only issue is that of the example
----------------------------------------------------------------------
dje · 2022-08-26 13:34
It sounds like you want the innerHTML of the P tag as text? The only
way I know how todo that is iterate the childnodes, convert each to a
string and then concat.
Here is a function to convert a TDOMNode to a string:
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
DOMNodeToString(A: TDOMNode): string; var LStream: TStringStream;
begin LStream := TStringStream.Create; try WriteXML(A,
LStream); Result := LStream.DataString; finally LStream.Free;
end; end; Here is a function to convert a TDOMNodeList to a
string:
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
DOMNodeListToString(A: TDOMNodeList): string; var I: integer; begin
Result := ''; for I := 0 to A.Count - 1 do Result := Result +
Trim(DOMNodeToString(A[I])); end;
And then with some very bad coding (example only) we can extract the
innerHTML of the P tag, with the various text nodes included:
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
ReadXMLFile(GXMLDocument, TStringStream.Create( '- lost in America.
- [man] He’s
coming!
'));
WriteLn(DOMNodeListToString(GXMLDocument.DocumentElement.ChildNodes));
Edit: If you only wanted certain tags to be included, the just filter
the node list during iteration.
----------------------------------------------------------------------
dje · 2022-08-26 13:36
O, I forgot: The output is:
Code: XML [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i -lost in America.
- [man] HeâÂÂs
coming!
----------------------------------------------------------------------
MarkMLl · 2022-08-26 13:51
Quote from: Espectr0 on August 26, 2022, 03:32:57 pmI understand that
it is well formed, and I read everything correctly, my only issue is
that of the example
As I've said, I'm not an expert: I'm just "trying to cover the bases"
as it were.
But I do note from https://stackoverflow.com/questions/7231902/self-
closing-tags-in-xml-files that it says
QuoteIn HTML there is always that third option of without
closing tag. I don't know about you, but several times a day, I have
to remind myself that HTML isn't just XML. And that actually means
that
and
aren't the same thing. You shouldn't use
in
HTML, and you shouldn't use
in XHTML.
without detailed explanation. So at the very least, this looks like a
contentious issue.
MarkMLl
----------------------------------------------------------------------
dje · 2022-08-26 14:20
I'm getting tired (Its 12 here), so I'll assume you wanted to extract
the subtitle entries from your example file. This is how I would do
it. I don't think there is any nicer way of extracting a mix of xml
and text, other than to iterate and filter.
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
ExtractEntry(A: TDOMNodeList): DOMString; var I: integer; S:
DOMString; begin Result := ''; for I := 0 to A.Count - 1 do
begin if SameText(A[I].NodeName, 'br') then begin //Result
:= Result + ' '; // Unsure what you wanted from a
tag? end
else begin if A[I] is TDOMText then begin S :=
Trim(A[I].TextContent); if Length(S) > 0 then begin
Result := Result + S + ' '; end; end else begin
Result := Result + ExtractEntry(A[I].ChildNodes); end;
end; end; end; procedure ExtractEntries(const AFileName:
TFileName); var LXMLDocument: TXMLDocument; LNode: TDOMNode;
LEntry: TDOMElement; LIndex: integer; begin
ReadXMLFile(LXMLDocument, AFileName); try LNode :=
LXMLDocument.DocumentElement.FindNode('body'); if Assigned(LNode)
then begin LNode := LNode.FindNode('div'); if
Assigned(LNode) then begin for LIndex := 0 to
LNode.ChildNodes.Count - 1 do begin if (LNode is
TDOMElement) and SameText(LNode.ChildNodes[LIndex].NodeName, 'p') then
begin LEntry := TDOMElement(LNode.ChildNodes[LIndex]);
WriteLn('>>> Region=', LEntry.AttribStrings['region'], '
Begin=', LEntry.AttribStrings['begin'], ' End=',
LEntry.AttribStrings['end']);
WriteLn(ExtractEntry(LNode.ChildNodes)); WriteLn;
end; end; end; end; finally
LXMLDocument.Free; end; end;
----------------------------------------------------------------------
Espectr0 · 2022-08-26 14:32
Thanks @dje and @MarkMLl!
@dje You got it right, I'm going to use your method
Thanks again!