PLATFORM:Lazarus 1.8.4, indyHTTP 10.6.2.0, OS: Windows Server 2016 (latest updates)
BACKGROUND:I am converting an old Delphi XE3 project to Lazarus. The Delphi XE3 project is able to get XML data from Data 24/7 and CDyne via HTTPS using Indy. All works correctly.
PROBLEM:I am unable to read specific nodes. For example, I want to read the value of the "status" node. Can't do it.
I am following the example here:
https://wiki.freepascal.org/XML_TutorialI have loaded the XMLDoc as follows: (Assume all try..excepts and try..finally 's and .Free 's are in place)
uses
Classes, SysUtils, StdCtrls, Forms, Controls,
idHTTP, idSSLOpenSSL, idURI,
DOM, laz2_DOM, laz2_XMLRead, laz2_XMLWrite, laz2_XMLCfg, laz2_XMLUtils, XMLStreaming, ...;
var
XMLNode: TDOMNode;
XMLDoc: TXMLDocument;
xml_mem_strm: TMemoryStream;
str_strm: TStringStream;
begin
...
xml_mem_strm := TMemoryStream.Create;
str_strm := TStringStream.Create('');
...
xHTTP.Get(GlobalVars.TxtMsg.https_get_cmd, xml_mem_strm);
xml_mem_strm.Position := 0;
ReadXMLFile(XMLDoc, xml_mem_strm);
This line converts the memory stream to a string and reveals that ALL the data EXISTS in the returned memory stream.
prc_log.Lines.Add(MemoryStreamToString(xml_mem_strm));
The resulting string from the above line is:
<?xml version="1.0"?><response><results><result item="1"><status>OK</status><number>15555555555</number><wless>n</wless><carrier_name>Telus Communications, Inc.</carrier_name><carrier_id>50106</carrier_id><sms_address> </sms_address><mms_address> </mms_address></result></results></response>
HERE IS WHAT I HAVE TRIED:
// Status Node Attempt 1 - Fails
idx := prc_log.Lines.Add('Selecting Status Node...');
Application.ProcessMessages;
XMLNode := XMLDoc.DocumentElement.FindNode('results/result(item=1)/status');
if not Assigned(XMLNode) then
begin
prc_log.Lines[idx] := prc_log.Lines[idx] + 'Can''t find it. Exiting now.';
snd_err := True;
Application.ProcessMessages;
Exit;
end;
GlobalVars.TxtMsg.xml_status := XMLNode.FirstChild.NodeValue;
prc_log.Lines[idx] := prc_log.Lines[idx] + 'Got it: ' + GlobalVars.TxtMsg.xml_status;
Application.ProcessMessages;
// Status Node Attempt 2 - Fails
idx := prc_log.Lines.Add('Selecting Status Node...');
Application.ProcessMessages;
XMLNode := XMLDoc.DocumentElement.FindNode('/response/results/result[@item=''1'']/status');
if not Assigned(XMLNode) then
begin
prc_log.Lines[idx] := prc_log.Lines[idx] + 'Can''t find it. Exiting now.';
snd_err := True;
Application.ProcessMessages;
Exit;
end;
GlobalVars.TxtMsg.xml_status := XMLNode.FirstChild.NodeValue;
prc_log.Lines[idx] := prc_log.Lines[idx] + 'Got it: ' + GlobalVars.TxtMsg.xml_status;
Application.ProcessMessages;
// Status Node Attempt 3 - Fails
idx := prc_log.Lines.Add('Selecting Status Node...');
Application.ProcessMessages;
XMLNode := XMLDoc.DocumentElement.FindNode('status');
if not Assigned(XMLNode) then
begin
prc_log.Lines[idx] := prc_log.Lines[idx] + 'Can''t find it. Exiting now.';
snd_err := True;
Application.ProcessMessages;
Exit;
end;
GlobalVars.TxtMsg.xml_status := XMLNode.FirstChild.NodeValue;
prc_log.Lines[idx] := prc_log.Lines[idx] + 'Got it: ' + GlobalVars.TxtMsg.xml_status;
Application.ProcessMessages;
MY QUESTION:What is the proper syntax to pull out the various node values, by using their node name, from the XML in the XMLDoc?
Thanks in advance.