Lazarus

Programming => Networking and Web Programming => Topic started by: BlueIcaro on July 09, 2021, 09:14:39 am

Title: [Solved] Reading xhtml+xml response: root missing
Post by: BlueIcaro 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
Title: Re: Reading xhtml+xml response: root missing
Post by: BlueIcaro 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
Title: Re: Reading xhtml+xml response: root missing
Post by: Mr.Madguy on July 09, 2021, 11:02:20 am
Try setting stream position to 0.
Title: Re: Reading xhtml+xml response: root missing
Post by: jamie 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
Title: [Solved] Reading xhtml+xml response: root missing
Post by: BlueIcaro 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