I'm going to make the presumption that once you find the Designation
, you're going to want to also read the other entries (Inner
, Outer
, Spin
, and Cage
) that go with the designation.
XPath
is the perfect solution for this problem. My example uses a new form with just a TMemo
and TButton
dropped on it, and adding a handler for the Button1.OnClick
event:
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 of the <Row>
elements, and loop through the information they contain, you can use this (add a second button to the test app 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;