Pra*_*eep 4 xml delphi delphi-2010
我有一个XML格式如下:
<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>
Run Code Online (Sandbox Code Playgroud)
在这里,使帐户独特的是属性ID和组合的组合City.
我怎么读这个Amount独特的?如何读取组合ID和City属性的金额?
例如,我需要Amount使用ID=225和获取帐户City=London.如果我使用代码
Node.GetAttribute('ID')=225
Run Code Online (Sandbox Code Playgroud)
它总是给我第一个ID = 225的节点
感谢您.
RRU*_*RUZ 12
尝试使用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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3187 次 |
| 最近记录: |