遍历 xml 并使用 vbscript 读取所有子节点值
Looping through the xml and read all child node values using 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>
下面的代码对我有用 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
我对 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>
下面的代码对我有用 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