I'm going to make the assumption that as soon as you find Designation , you will also want to read the other notes ( Inner , Outer , Spin and Cage ) that come with the designation.
XPath is the perfect solution to this problem. My example uses a new form with TMemo and TButton just dropped and the TButton event handler added:
uses MSXML, ComObj, ActiveX; const XMLText = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + '<Data>' + '<Row>' + '<Designation>1234102</Designation>' + '<Inner>5.412</Inner>' + '<Outer>3.588</Outer>' + '<Spin>4.732</Spin>' + '<Cage>0.399</Cage>' + '</Row>' + '<Row>' + '<Designation>1342153</Designation>' + '<Inner>5.916</Inner>' + '<Outer>4.084</Outer>' + '<Spin>5.277</Spin>' + '<Cage>0.408</Cage>' + '</Row>' + '</Data>'; procedure TForm1.Button1Click(Sender: TObject); var XMLDoc: IXMLDOMDocument; Node, SibNode: IXMLDOMNode; begin Memo1.Clear; XMLDoc := CoDOMDocument.Create; XMLDoc.loadXML(XMLText); // Select the node with the Designation you want. Node := XMLDoc.selectSingleNode('//Designation[text()="1342153"]'); if Assigned(Node) then begin Memo1.Lines.Add('Found it.'); Memo1.Lines.Add(Node.nodeName + ' = ' + Node.firstChild.nodeValue); // Read all the nodes at the same level as the Designation SibNode := Node.nextSibling; while SibNode <> nil do begin Memo1.Lines.Add(SibNode.nodeName + ' = ' + SibNode.firstChild.nodeValue); Sib := Sib.nextSibling; end; end; end;
If you want to just grab all the <Row> elements and skip the information contained in them, you can use this (add a second button to the test application above and use this for the Button2.OnClick handler).
procedure TForm1.Button2Click(Sender: TObject); var XMLDoc: IXMLDOMDocument; NodeList: IXMLDOMNodeList; Node, SibNode: IXMLDOMNode; i: Integer; begin Memo1.Clear; XMLDoc := CoDOMDocument.Create; XMLDoc.loadXML(XMLText); NodeList := XMLDoc.selectNodes('/Data/Row'); if Assigned(NodeList) then begin for i := 0 to NodeList.length - 1 do begin Node := NodeList.item[i]; SibNode := Node.firstChild; while Assigned(SibNode) do begin Memo1.Lines.Add(SibNode.nodeName + ' = ' + SibNode.firstChild.nodeValue); SibNode := SibNode.nextSibling; end; end; // Add a blank line between groupings for readability Memo1.Lines.Add(''); end; end;
source share