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 !
X-Ray
March 29, 2009, March 29, 2009 14:54, permalink
you could use a different xml parser. i think sax is faster.
Wouter van Nifterick
March 5, 2010, March 05, 2010 02:49, permalink
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;
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.