Ec72b0ab3eb1855c56fd04bcc886f65c

This is intended to be a helper method to simply extract the Text contents of the first XML Node that it finds within an XML Document with the name as specified in ElementName. In reality this is used when we are only expecting 1 node of a certain name in a Document.

I would like to see some refactorings based on speeding it up or versions allowing a XML Node to be passed to the function to extract the Text contents of a sub element of just that Node.

function getFirstElementTextByName(ElementName: string; const XMLDoc: IXMLDocument): string;
var
  NodeList: IDOMNodeList;
  Node: IDOMNode;
begin
  Result := '';
  NodeList := XMLDoc.DOMDocument.getElementsByTagName(ElementName);
  if NodeList.length > 0 then
    begin
      if NodeList.item[0].hasChildNodes then Node := NodeList.item[0].firstChild else Node := NodeList.item[0];
      Result := Node.nodeValue;
    end;
end;

Refactorings

No refactoring yet !

92647e77af9606a13dc6ccc39dc37265

X-Ray

March 29, 2009, March 29, 2009 14:54, permalink

No rating. Login to rate!

you could use a different xml parser. i think sax is faster.

45af96802dc2ea89356c254bd9d0af06

Wouter van Nifterick

March 5, 2010, March 05, 2010 02:49, permalink

No rating. Login to rate!

I didn't really look into the body of the function yet, but if you want speed you shouldn't pass strings by value.

function getFirstElementTextByName(const ElementName: string; const XMLDoc: IXMLDocument): string;
var
  NodeList: IDOMNodeList;
  Node: IDOMNode;
begin
  Result := '';
  NodeList := XMLDoc.DOMDocument.getElementsByTagName(ElementName);
  if NodeList.length > 0 then
    begin
      if NodeList.item[0].hasChildNodes then Node := NodeList.item[0].firstChild else Node := NodeList.item[0];
      Result := Node.nodeValue;
    end;
end;

Your refactoring





Format Copy from initial code

or Cancel