循环遍历xml并使用vbscript读取所有子节点值
我对VB脚本编写相当陌生,并尝试通过解析xml响应中的每个属性块来读取xml。我确实看到过其他有例子的帖子,但在这里找不到任何直接的帮助解决这个问题的东西。其他帖子中的xml结构是不同的,因此这不是一个重复的问题。下面是我开始编写的代码,在获取节点数之前,我可以获取这些代码,但无法读取这些值循环遍历xml并使用vbscript读取所有子节点值,xml,vbscript,xml-parsing,Xml,Vbscript,Xml Parsing,我对VB脚本编写相当陌生,并尝试通过解析xml响应中的每个属性块来读取xml。我确实看到过其他有例子的帖子,但在这里找不到任何直接的帮助解决这个问题的东西。其他帖子中的xml结构是不同的,因此这不是一个重复的问题。下面是我开始编写的代码,在获取节点数之前,我可以获取这些代码,但无法读取这些值 Set xmlDoc = CreateObject("MSXML2.DOMDocument") xmlDoc.setProperty "SelectionLanguage", "XPath" xmlDoc.
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
试验
GETCUST
0755970501
2019-09-10
2019-12-12
20000
成功
0
2.
102
2019-11-11
104
2019-12-12
下面的代码适用于具有如下结构的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