This is some sample code what process XML in an http request with Indy. I have omitted a lot of object allocation code for brevity sake, but you should be able to work it out.
xdrEventServer is TIdHttpServer component and xdrEventServerCommandGet is its request processing event, OnCommandGet.
ARequestInfo.PostStream.Position := 4
This is used because the XML at 5 characters into the data.
The code receives a request, parses out the XML body and prints out the XML keys which are loaded in InitKeyStrings. You will probably need some of these units in your program.
IdCustomTCPServer, IdContext, IdTCPClient, IdComponent,
IdHTTPServer, IdAntiFreeze, IdCustomHTTPServer, IdURI, DOM, xmlread
If you are going to use XPath get a more recent version as there are bugs in the older implementations. Check the attachment and
http://lists.freepascal.org/lists/fpc-pascal/2010-May/024929.htmlprocedure TdmSocketExplorer.xdrEventServerCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
postStrings, valStrings, keyStrings: TStringList;
XML: TXMLDocument;
strStream, xdrStream: TStringStream;
xdrString: ansistring;
procedure InitKeyStrings;
begin
keyStrings.AddStrings(uIF.mmoXDRKeys.Lines);
end;
procedure AddToDisplayMemo;
var
i: integer;
begin
uIF := utilitiesInterfaceForm;
uIF.mmoXDRXML.Lines.Clear;
uIF.mmoXDRXMLHistory.Lines.Add('== xdrString - Start ==');
for i := 0 to keyStrings.Count - 1 do
begin
uIF.mmoXDRXML.Lines.Add(keyStrings.Strings[i] + ' => ' + valStrings.Strings[i]);
uIF.mmoXDRXMLHistory.Lines.Add(keyStrings.Strings[i] + ' => ' + valStrings.Strings[i]);
end;
uIF.mmoXDRXML.Lines.Add('==xdr XML end==');
uIF.mmoXDRXMLHistory.Lines.Add('== xdrString - End ==');
uIF.mmoXML.Lines.Add('== xdr XML start ==');
uIF.mmoXML.Lines.AddStrings(postStrings);
uIF.mmoXML.Lines.Add('== xdr XML end ==');
end;
begin
InitializeObjects;
ARequestInfo.PostStream.Position := 4;
postStrings.LoadFromStream(ARequestInfo.PostStream);
postStrings.Text := TIdUri.URLDecode(postStrings.Text);
xdrStream := TStringStream.Create(postStrings.Text);
GetXMLValues(xdrStream, keyStrings, valStrings);
AddToDisplayMemo;
DestroyObjects;
end;
procedure GetXMLValues(xdrStream: TStringStream; keyStrings: TStringList; valStrings: TStringList);
var
XML: TXMLDocument;
i, j: integer;
nodeList: TDomNodeList;
begin
ReadXMLFile(XML, xdrStream);
for j := 0 to keyStrings.Count - 1 do
begin
nodeList := XML.GetElementsByTagName(keyStrings.Strings[j]);
if nodeList.Count > 0 then
begin
for i := 0 to 0 do //I need only the first matching item
begin
valStrings.Add(TIdURI.Urldecode(nodeList[i].TextContent));
end
end else
begin
valStrings.Add('<null>');
end;
nodeList.Free;
end;
XML.Free;
end;
LeleDumbo you got the reason, but, i have use Indy 10 , you know how i can work with that??