【发布时间】:2016-02-02 14:37:13
【问题描述】:
XML 文件:
<Partner>
...
</Partner>
<Partner>
<K1>10</K1>
<K2>3</K2>
<K3>5254304</K3>
<K4>test name</K4>
<K5>637.51</K5>
<K6>159.38</K6>
<K7>802.39</K7>
<K8>0.00</K8>
<K9>802.39</K9>
<Invoices>
<Invoice>
<R1>1</R1>
<R2>4-02R0113-12</R2>
<R3>2014-12-29</R3>
<R4>2014-12-29</R4>
<R5>398</R5>
<R6>637.51</R6>
<R7>159.38</R7>
<R8>802.39</R8>
<R9>0.00</R9>
<R10>802.39</R10>
</Invoice>
</Invoices>
</Partner>
<Partner>
...
</Partner>
在我的 XML 文件中,我有重复节点 <Partner>。每个合作伙伴都有自己的标识号写入节点<K3>。
每个合作伙伴都可以拥有多张发票。
我需要在发票中的 <R6> 和 <R7> 中查找和读取值,其中
我知道<R2>、<R3>、<R8> 的值。
在搜索条件为字段<K3> 的合作伙伴的多个字段<R2>、<R3>、<R8> 的情况下,我如何搜索特定发票并获取<R6> 和<R7> 的字段值?
如何给SelectSingleNode添加多条件?
我的代码:
procedure TfrmTest.TestReadOmniXML;
var
xml: IXMLDocument;
iNodePartner, iNodePartnerInvoice, iNodePartnerInvoiceR6, iNodePartnerInvoiceR7 : IXMLNode;
begin
xml := CreateXMLDoc;
xml.Load('c:\test.xml');
iNodePartner := XML.SelectSingleNode('//Partner[K3=' + '0254304' + ']');
iNodePartnerInvoice := iNodePartner.SelectSingleNode(
//single query works OK
'.//Racuni/Racun[R2=' + '4-02R0113-12' + ']'
//but I need to add these fields also
// ' and [R3=' + '2014-12-29' + ']' +
// ' and [R8=' + '802.39' + ']'
);
if Assigned( iNodePartnerInvoice ) then
begin
iNodePartnerInvoiceR6 := iNodePartnerInvoice.SelectSingleNode('./R6');
Label1.Caption := iNodePartnerInvoiceR6.Text;
iNodePartnerInvoiceR7 := iNodePartnerInvoice.SelectSingleNode('./R7');
Label2.Caption := iNodePartnerInvoiceR7.Text;
end;
...
end;
【问题讨论】:
-
IIRC,这行得通:'.//Racuni/Racun[R2=' + '4-02R0113-12' + ']' + ' [R3=' + '2014-12-29' + ']' + '[R8=' + '802.39' + ']'
-
我刚刚试了一下。对我来说它不起作用。节点为零。