首页 > 解决方案 > 循环通过 xml 并使用 vbscript 读取所有子节点值

问题描述

我对 VB 脚本相当陌生,并试图通过解析 xml 响应中的每个属性块来读取 xml。我确实看到了其他带有示例的帖子,但在这里找不到任何有助于解决此问题的直接内容。其他帖子中的 xml 结构不同,因此这不是一个重复的问题。下面是我开始的代码,可以得到直到获得节点数但无法读取值..

Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load "C:\TestData\Blhdr_sampleresp_test.xml"
Set objNodeList = xmlDoc.getElementsByTagName("Blhd_cust_head_detail")
If objNodeList.length > 0 then
    For each x in objNodeList
                    ' The below code line works but I would like to be able get the value of specified node
        Set objNode = objXMLDoc.documentElement.lastChild
        Print objNode.text
        'blhdnum=x.getAttribute("Blhd_cust_head_nb")
        'Print blhdnum
    Next
Else
    Print " No child nodes  found."
End If

这是我拥有的示例 xml:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <GetBillCustResponse xmlns="http://www.test.com/test3">
         <GetBillCustResult>
            <Blhd_request>
               <User_id>User1</User_id>
               <Origin_sys_nm>TEST</Origin_sys_nm>
               <Operation_nm>GETCUST</Operation_nm>
               <Blhd_rqst_data>
                  <Blhd_cust_acct_nb>0755970501</Blhd_cust_acct_nb>
                  <Blhd_cust_start_dt>2019-09-10</Blhd_cust_start_dt>
                  <Blhd_cust_stop_dt>2019-12-12</Blhd_cust_stop_dt>
               </Blhd_rqst_data>
            </Blhd_request>
            <Blhd_return>
               <Response_cd>20000</Response_cd>
               <Response_desc_tx>SUCCESS</Response_desc_tx>
               <Err_cnt>0</Err_cnt>
               <Error_details/>
               <Blhd_resp_data>
                  <Blhd_cust_head_cnt>2</Blhd_cust_head_cnt>
                  <Blhd_cust_head_detail>
                     <Blhd_cust_head_nb>102</Blhd_cust_head_nb>
                     <Blhd_cust_dt>2019-11-11</Blhd_cust_dt>
                  </Blhd_cust_head_detail>
                  <Blhd_cust_head_detail>
                     <Blhd_cust_head_nb>104</Blhd_cust_head_nb>
                     <Blhd_cust_dt>2019-12-12</Blhd_cust_dt>
                  </Blhd_cust_head_detail>
               </Blhd_resp_data>
            </Blhd_return>
         </GetBillCustResult>
      </GetBillCustResponse>
   </soap:Body>
</soap:Envelope>

标签: xmlvbscriptxml-parsing

解决方案


下面的代码适用于具有这样结构的 xml。

strFileName = "C:\TestData\Blhdr_sampleresp_test.xml"
Set objDoc = CreateObject("Microsoft.XMLDOM")
objDoc.Async = False
objDoc.Load strFileName

'Get all the orderlist nodes

Set objNodeList = objDoc.SelectNodes("//Blhd_cust_head_detail")
Print objNodeList.length

'Loop through each of the returned nodes in the nodelist

 For Each objNode In objNodeList
    Set objSubNode = objNode.SelectSingleNode("Blhd_cust_head_nb")
    Print objSubNode.Text

    Set objSubNode = objNode.SelectSingleNode("Blhd_cust_dt")
    Print objSubNode.Text
Next

'Cleanup

Set objNodeList = Nothing
Set objDoc = Nothing

推荐阅读