试试XPath,用这句话./Accounts/Account[@ID="225"][@City="London"]
定位节点。
试试这个样本
{$APPTYPE CONSOLE}
uses
MSXML,
SysUtils,
ActiveX,
ComObj;
Const
XmlStr =
' <Accounts>'+
' <Account ID ="1" City="Bangalore" Amount="2827561.95"/>'+
' <Account ID="225" City="New York" Amount="12312.00"/>'+
' <Account ID="236" City="London" Amount="457656.00"/>'+
' <Account ID="225" City="London" Amount="23462.40"/>'+
' <Account ID="236" City="Bangalore" Amount="2345345.00"/>'+
'</Accounts>';
procedure Test;
Var
XMLDOMDocument : IXMLDOMDocument;
XMLDOMNode : IXMLDOMNode;
begin
XMLDOMDocument:=CoDOMDocument.Create;
XMLDOMDocument.loadXML(XmlStr);
XMLDOMNode := XMLDOMDocument.selectSingleNode(Format('./Accounts/Account[@ID="%s"][@City="%s"]', ['225', 'London']));
if XMLDOMNode<>nil then
Writeln(Format('Amount %s',[String(XMLDOMNode.attributes.getNamedItem('Amount').Text)]));
end;
begin
try
CoInitialize(nil);
try
Test;
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.