Link to home
Start Free TrialLog in
Avatar of Adriaan Boshoff
Adriaan Boshoff

asked on

Reading XML data if there is no top level element in delphi

I'm trying to read an xml response from a api but I keep getting the following error:


How can I read the xml data if the response looks like this:
<status>success</status><statusmsg>online</statusmsg><vmstat>online</vmstat><hostname>quantum.vps</hostname><ipaddress>xxx.xx.xx.xxx</ipaddress>

Open in new window


My current code:
procedure TfrmMain.btn1Click(Sender: TObject);
var
  httpclient: TIdHTTP;
  doc: TXMLDocument;
  data: IXMLNode;
begin
  httpclient := TIdHTTP.Create(nil);
  try
    doc := TXMLDocument.Create(nil);
    try
      doc.XML.Text := httpclient.Get('https://site.com/api.php?action=status'); //Modified URL for experts exchange
      doc.Active := True;
      data := doc.DocumentElement;
      lbl1.Text := data.ChildNodes['status'].Text;
    finally
      doc.Free;
    end;
  finally
    httpclient.Free;
  end;
end;

Open in new window

Avatar of aikimark
aikimark
Flag of United States of America image

Try something like this:
      doc.XML.Text := '<root>' + httpclient.Get('https://site.com/api.php?action=status') + '</root>'; //Modified URL for experts exchange

Open in new window

Avatar of Adriaan Boshoff
Adriaan Boshoff

ASKER

@aikimark Now it just gives me a invalid pointer operation.
ASKER CERTIFIED SOLUTION
Avatar of Adriaan Boshoff
Adriaan Boshoff

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, you changed statement 11 and got an invalid pointer operation?
i'd use PosEx instead
it allows you to search from a specific position

and thus allows to return multiple items within same tags
You would have a better solution if you used the regular expression object to do your parsing.  You could populate and return a TDictionary (hash table) of the tags and their values from a single function call.
I found this to be easier and faster.