Archive

Archive for January, 2008

Sample function for reading XML node value

January 2nd, 2008

I’ve had many problems using the IXMLDocument object, mostly related to different versions of the .net redistributable installed on different machines. So, I wrote a short function that extracts a node value from the passed XML string:

function GetNodeVal(sNode, sXML : string) : string ;
{This function extracts the specified xml node's value from the XML string}

var
  iPos : integer ;
  sValue : string ;

begin
  iPos := Pos('<'+ sNode + '>', sXML) ;

  if iPos = 0 then
  begin
    iPos := Pos('<'+ Lowercase(sNode) + '>', sXML) ;

    if iPos = 0 then
    begin
      result := 'Error:Globals.GetNodeVal' ;
      exit ;
    end;
  end;

  sValue := Copy(sXML, iPos + 2 + Length(sNode), 255) ;

  iPos := Pos('</' + sNode + '>', sValue) ;

  if iPos = 0 then
  begin
    iPos := Pos('</'+ Lowercase(sNode) + '>', sValue) ;

    if iPos = 0 then
    begin
      result := 'Error:Globals.GetNodeVal' ;
      exit ;
    end;
  end;

  sValue := Copy(sValue, 1, iPos -1) ;
  result := sValue ;
end;

Delphi