You can use XPath to select all authors using an expression like //catalog/subject/book/author , from delphi you have a lot of options for managing the XML file, but in this case it is easiest to use the Microsoft XML DOM implementation
check out this sample application
{$APPTYPE CONSOLE} uses ActiveX, Variants, ComObj, SysUtils; procedure ReadXMLFile(const FileName:TFileName); const Msxml2_DOMDocument='Msxml2.DOMDocument.6.0'; var XmlDoc : OleVariant; Nodes : OleVariant; lNodes : Integer; i : Integer; begin //create an instance to the XML DOM XmlDoc := CreateOleObject(Msxml2_DOMDocument); try XmlDoc.Async := False; //load the file XmlDoc.Load(FileName); //set the xpath mode XmlDoc.SetProperty('SelectionLanguage','XPath'); //check for errors in the xml file if (XmlDoc.parseError.errorCode <> 0) then raise Exception.CreateFmt('Error in Xml Data %s',[XmlDoc.parseError]); //select the nodes with match with the expression Nodes := XmlDoc.selectNodes('//catalog/subject/book/author'); //get the number of nodes selected lNodes:= Nodes.Length; Writeln(Format('%d Authors found',[lNodes])); //traverse the nodes for i:=0 to Nodes.Length - 1 do Writeln(Format('Author Name %s',[Nodes.Item(i).Text])); finally XmlDoc :=Unassigned; end; end; begin try CoInitialize(nil); try ReadXMLFile(ExtractFilePath(ParamStr(0))+'test.xml'); finally CoUninitialize; end; except on E:Exception do Writeln(E.Classname, ':', E.Message); end; Writeln('Press Enter to exit'); Readln; end.
finally the application will return this
12 Authors found Author Name Gambardella, Matthew Author Name Galos, Mike Author Name Ralls, Kim Author Name Corets, Eva Author Name Corets, Eva Author Name Corets, Eva Author Name Randall, Cynthia Author Name Thurman, Paula Author Name Knorr, Stefan Author Name Kress, Peter Author Name O'Brien, Tim Author Name O'Brien, Tim
source share