Txmldocument load from a String

Asked

Viewed 951 times

1

I’m new to Delphi and I’m having trouble loading an XML document from a string. There is no mistake, just does not load, it makes me think I must be making some very 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.

Inside var Result returns 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?

  • Result:<? xml version="1.0" encoding="GBK"? >'#$A'<Alipay><is_success>F</is_success><error>ILLEGAL_PARTNER</error></Alipay>

  • There is an extra space inside the URL here: gateway.do? service=notify_verify. I don’t think this space should be there.

3 answers

0

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

Test 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;

0

Actually the XML was loading, but I thought it was not because I received a null Pointer when I tried to get Node 'Alipay' from the list of nodes that returns on 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.

0

Your Result variable has to be of the Widestring type and try to use the Ixmldocument type:

Result : WideString;

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.