TXMLDocument load from a String

1

I'm new to Delphi and am having trouble loading an XML document from a string. It does not make any mistake, it just does not load, it makes me think that I must be making some pretty silly mistake.

My code is this:

var
  lHTTP: TIdHTTP;
  Result: String;
  doc: TXMLDocument;
  alipay: IXMLNodeList;

begin
  lHTTP := TIdHTTP.Create;
  doc := TXMLDocument.Create(nil);
  try
    Result := lHTTP.Get('https://mapi.alipay.com/gateway.do? service=notify_verify&partner=002153&notify_id=589654');

    doc.LoadFromXML(Result);
    doc.Active := True;
    alipay := doc.ChildNodes;

  finally
    lHTTP.Free;
  end;
end;

end.

Within the var Result returns the XML:

<?xml version="1.0" encoding="GBK"?>'#$A'<alipay><is_success>F</is_success> 
<error>ILLEGAL_PARTNER</error></alipay>

Does anyone know what's going on?

    
asked by anonymous 15.08.2018 / 22:43

3 answers

0

You can change your code to use another class to work with XML, using IXMLDocument .

Try this:

var
  lHTTP: TIdHTTP;
  Result: String;
  doc: IXMLDocument; { alterado para "I" em vez de "T" }
  alipay: IXMLNodeList;

begin
  lHTTP := TIdHTTP.Create;
  doc := TXMLDocument.Create(nil);
  try
    Result := lHTTP.Get('https://mapi.alipay.com/gateway.do? service=notify_verify&partner=002153&notify_id=589654');

   doc.loadXML(Result); { alterada a função de carregamento do XML }
   doc.Active := True;
   alipay := doc.ChildNodes;

   finally
     lHTTP.Free;
   end;
end;
    
15.08.2018 / 22:58
0

Actually the XML was loading, but I thought it was not because I was getting a null pointer when I tried to get the node 'alipay' from the list of nodes that returns in this line:

 alipay := doc.ChildNodes;

But I managed to find a solution:

var
  lHTTP: TIdHTTP;
  Result: String;
  doc: TXMLDocument;
  alipayNodes: IXMLNodeList;
  xmlNodes: IXMLNodeList;
  node: IXMLNode;
  sSuccess: String;
  sError: String;
  i: Integer;

begin
  lHTTP := TIdHTTP.Create;
  doc := TXMLDocument.Create(nil);
  try
    Result := lHTTP.Get('https://mapi.alipay.com/gateway.do?service=notify_verify&partner=002153&notify_id=589654');
    doc.LoadFromXML(Result);
    doc.Active := True;
    xmlNodes := doc.ChildNodes;

    if xmlNodes.Count >= 2 then
    begin
      alipayNodes := xmlNodes.Get(1).ChildNodes;;
    end;

    i := 0;
    while i < alipayNodes.Count do
    begin
      node := alipayNodes.Get(i);
      if AnsiCompareText(node.LocalName, 'is_success') = 0 then
        sSuccess := node.Text;
      if AnsiCompareText(node.LocalName, 'error') = 0 then
        sError := node.Text;
      i := i + 1;
    end;

  finally
    lHTTP.Free;
  end;
end;

end.
    
16.08.2018 / 00:44
0

Your Result variable must be of type WideString and try to use the type IXMLDocument:

Result : WideString;

var
 doc : IXMLDocument;
 Result : WideString;
 alipay, alipay2 : IXMLNode;
begin
 doc.LoadFromXml(Result);
 alipay := doc.DocumentElement;
 alipay2 := alipay.ChildNodes.First;
    
07.09.2018 / 05:02