遍历xml并使用vbscript读取所有子节点值

时间:2020-02-26 17:03:45

标签: xml vbscript xml-parsing

我对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>

1 个答案:

答案 0 :(得分:0)

以下代码对具有这样结构的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