XML 和 VBS - 访问孙节点



首先,我是一名大型机程序员。我正在尝试编写一个 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

附加到此行,我想要每个日期直接借记和金额值。

现在,我可以毫无问题地得到第一批东西。我似乎无法得到的是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

如果只有一个这样的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

最新更新