Recent

Author Topic: [Solved] Reading xhtml+xml response: root missing  (Read 3421 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 660
    • My github
[Solved] Reading xhtml+xml response: root missing
« on: July 09, 2021, 09:14:39 am »
Hello I tryng to read a xhtml+file response from a webserve.
Here is a example of the response:
Quote
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Services</title>
  <base href="http://localhost:80/" /> </head>

<body>
  <div class="state"> <a href="" rel="self" />
    <ul>
      <li class="srvlst-service-li" title="ctrl"> <a href="ctrl" rel="self"> </a> </li>
      <li class="srvlst-service-li" title="rw"> <a href="rw" rel="self"> </a> </li>
      <li class="srvlst-service-li" title="progress"> <a href="progress" rel="self"> </a> </li>
      <li class="srvlst-service-li" title="fileservice"> <a href="fileservice" rel="self"> </a> </li>
      <li class="srvlst-service-li" title="users"> <a href="users" rel="self"> </a> </li>
      <li class="srvlst-service-li" title="subscription"> <a href="subscription" rel="self"> </a> </li>
    </ul>
  </div>
</body>

</html>

But When I try to read with ReadXMLFile procedure I got a excepcion: "Root element missing".
I use this code:
Code: [Select]
var
  F: TMemoryStream;
  DOC: TXMLDocument;
begin
 F := TMemoryStream.Create;
 F.WriteAnsiString(mmEntrada.Lines.Text);
 ReadXMLFile(doc,F);
 doc.Free;
 F.Free;
end;
   

May be I using a wrong method?. How can I read and work with this response from web?
I attach a example program
Thanks
/BlueIcaro
« Last Edit: July 22, 2021, 12:48:55 pm by BlueIcaro »

BlueIcaro

  • Hero Member
  • *****
  • Posts: 660
    • My github
Re: Reading xhtml+xml response: root missing
« Reply #1 on: July 09, 2021, 10:31:53 am »
Hi, I found that if I read the xml response from a file, I don't have any error.  :(

/BlueIcaro

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 687
Re: Reading xhtml+xml response: root missing
« Reply #2 on: July 09, 2021, 11:02:20 am »
Try setting stream position to 0.
30.04.2021 - DynamicData 4.0 is released and migration to it is completed.
It's more Lazarus-friendly, but still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 12 years old.

jamie

  • Hero Member
  • *****
  • Posts: 4841
Re: Reading xhtml+xml response: root missing
« Reply #3 on: July 09, 2021, 12:37:54 pm »
Try setting stream position to 0.

yes, that looks exactly the issue.. Good catch if not the issue  :D
The only true wisdom is knowing you know nothing

BlueIcaro

  • Hero Member
  • *****
  • Posts: 660
    • My github
[Solved] Reading xhtml+xml response: root missing
« Reply #4 on: July 22, 2021, 12:37:02 pm »
Hello, Finally I found the solution:
Code: [Select]

var
  Cadena: string;
  s: TStringStream;
  XmlDoc: TXMLDocument;
  hijo: TDOMNodeList;
begin
  Cadena := mmEntrada.Lines.Text;
  s := TStringStream.Create(Cadena);
  ReadXMLFile(XmlDoc, s);
  hijo := XmlDoc.GetChildNodes;
  if hijo <> nil then
    mmResultado.Lines.Add('Tiene hijos');
end;   

/BlueIcaro

 

TinyPortal © 2005-2018