XML和VBS - 访问孙子节点

时间:2014-12-24 11:45:32

标签: xml vb.net

首先,我是一名大型机程序员。我正在尝试编写一个Windows VB脚本来从多个文件中提取信息以进行诊断/调查,但我遇到了困难。 XML用于英国BACS关于直接借记纠纷和外观(浏览器操纵)的报告,如

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!--  Generated by Oracle Reports version 10.1.2.3.0  --> 
<VocaDocument xsi:noNamespaceSchemaLocation="VOCALINK_DDICAdvice.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Data>
  <Document type="DIRECT DEBIT INDEMNITY CLAIM ADVICE REPORT" created="2014-10-31T01:28:02" schemaVersion="1.0">
  <CompanyName>Bacs Payment Schemes Limited</CompanyName> 
  <ReportTitle>DIRECT DEBIT INDEMNITY CLAIM ADVICE REPORT FOR 30/10/2014</ReportTitle> 
  <ReportProductionDate>2014-10-31T01:28:02</ReportProductionDate> 
  <ServiceUserNumber>987654</ServiceUserNumber> 
  <ServiceUserName>THE COMPANY LIMITED</ServiceUserName> 
  <NumberOfAdvices>1</NumberOfAdvices> 
  <NewAdvices>
   <DDICAdvice>
    <SeqNo>2014102903A987654321</SeqNo> 
    <PayingBankReference>DDICRBOSABC123</PayingBankReference> 
    <SUNumber>999888</SUNumber> 
    <SUReference>ABC12300000</SUReference> 
    <ReasonCode>2</ReasonCode> 
    <PayerSortCode>123456</PayerSortCode> 
    <PayerAccount>987654</PayerAccount> 
    <PayerName>CUSTOMER</PayerName> 
    <NoOfAdvForClaim>1</NoOfAdvForClaim> 
    <TotalAmount>122.65</TotalAmount> 
    <DDCollections>
     <DDCollection>
      <DateOfDirectDebit>2014-10-16</DateOfDirectDebit> 
      <Amount>122.65</Amount> 
     </DDCollection>
    </DDCollections>
   </DDICAdvice>
   <TotalNumberOfNewAdvices>1</TotalNumberOfNewAdvices> 
   <TotalValueOfDebits>122.65</TotalValueOfDebits> 
   <DateOfDebit>2014-11-19</DateOfDebit> 
  </NewAdvices>
  <ReasonCodeMeaning>1 = Amount and / or date of Direct Debit differ from Advance Notice.2 = No Advance Notice received by Payer/or the amount quoted is disputed.3 = DDI cancelled by paying bank.4 = Payer has cancelled DDI direct with service user.5 = AUDDIS service users only - No Instruction held. Payer disputes having given authority.6 = AUDDIS service users only - Signature on DDI is fraudulent or not in accordance with account authorised signature(s).7 = Claim raised at service users request after Direct Debit applied to payers account.8 = Service user name disputed. Payer does not recognise service user collecting Direct Debit.</ReasonCodeMeaning> 
  </Document>
 </Data>
</VocaDocument>

我想输出的是包含特定值的CSV文件 的SeqNo,PayingBankReference,SUReference,ReasonCode,PayerSortCode,PayerAccount,PayerName

附加到此行我想要每个DateOfDirectDebit和Amount值。

现在,我可以得到第一个没问题的东西。我似乎无法得到的是DDCollections / DDCollection子循环。谁能给我一些指示?

作为参考,我为每个文件使用的代码是(我尝试了几种变体):

For Each advice In xmlDoc.documentElement.selectNodes("/VocaDocument/Data/Document/NewAdvices/DDICAdvice")
  SeqNo = advice.selectSingleNode("SeqNo").Text
  PayingBankReference = advice.selectSingleNode("PayingBankReference").Text
  SUReference = advice.selectSingleNode("SUReference").Text
  ReasonCode = advice.selectSingleNode("ReasonCode").Text
  PayerSortCode= advice.selectSingleNode("PayerSortCode").Text
  PayerAccount= advice.selectSingleNode("PayerAccount").Text
  PayerName= advice.selectSingleNode("PayerName").Text
  TotalAmount = advice.selectSingleNode("TotalAmount").Text
  outLine = SeqNo & "," & PayingBankReference & "," & SUReference & "," & ReasonCode & "," & PayerSortCode & "," & PayerAccount & "," & PayerName & ",""" & TotalAmount & """"
  For each ddCollection In advice.ChildNodes
    if ddCollection.nodeName = "DateOfDirectDebit" then outline = outLine & "," & ddCollection.Text
    if ddCollection.nodeName = "Amount" then outline = outLine & ",""" & ddCollection.Text & """"
  Next
  objTextFile.WriteLine(OutLine)
Next

1 个答案:

答案 0 :(得分:0)

如果只有一个这样的DDCollection孩子,那么你可以用Advice元素来抓住孩子,即:

DateOfDirectDebit = advice.selectSingleNode("DDCollections/DDCollection/DateOfDirectDebit").Text
Amount = advice.selectSingleNode("DDCollections/DDCollection/Amount").Text

如果有多个子DDCollection元素,则您需要进行迭代 - 您可以再次使用selectNodes

For each ddCollection In advice.selectNodes("DDCollections/DDCollection")
  DateOfDirectDebit = advice.selectSingleNode("DateOfDirectDebit").Text
  Amount = advice.selectSingleNode("Amount").Text
  ** Concatenate here ...
Next