【发布时间】:2019-05-17 03:01:19
【问题描述】:
我正在开发一个使用 SOAP 调用将请求数据导入 Excel 的项目。我可以返回 XML 没有问题,它看起来像这样。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<RetrieveRequestsResponse xmlns="http://rapid2.library.colostate.edu/rapid5api/">
<RetrieveRequestsResult>
<IsSuccessful>true</IsSuccessful>
<Transactions>
<Transaction>
<RapidRequestId>1111</RapidRequestId>
<XRefRequestId>[TN:11111]</XRefRequestId>
<StateType>Pending</StateType>
<RapidRequestType>Article</RapidRequestType>
<BorrowerRapidCode>###</BorrowerRapidCode>
<BorrowerCountryCode>AU</BorrowerCountryCode>
</Transaction>
</Transactions>
</RetrieveRequestsResult>
</RetrieveRequestsResponse>
</soap:Body>
</soap:Envelope>
当我尝试在 Excel 中解析和显示 XML 时出现问题。
使用this excellent tutorial我创建了以下代码
Dim Resp As New DOMDocument60
Resp.LoadXML xmlhtp.responseText
Dim transaction As IXMLDOMNode
Dim XmlNamespaces As String
Dim i As Integer
XmlNamespaces = "xmlns:doc2='http://rapid2.library.colostate.edu/rapid5api/'
Resp.setProperty "SelectionNamespaces", XmlNamespaces
For Each transaction In Resp.getElementsByTagName("Transaction")
Debug.Print "tesst"
i = i + 1
WS.Range("A2:A200").Cells(1, i).Value = transaction.SelectNodes("//doc2:RapidRequestId")(0).Text
Next Transaction
End With
End Sub
但是,这会在 Locals 中为事务返回“无”,并在我调试时跳过循环。
经过进一步研究I came across this post 似乎解决了我的未声明命名空间的问题?
<RetrieveRequestsResponse xmlns="http://rapid2.library.colostate.edu/rapid5api/">
在对我的代码进行了一些漫无目的的调整之后,我最终得到了;
Dim Resp As New DOMDocument60
Resp.LoadXML xmlhtp.responseText
Dim Transaction As IXMLDOMNode
Dim XmlNamespaces As String
Dim i As Integer
XmlNamespaces = "xmlns:doc2='http://rapid2.library.colostate.edu/rapid5api/'"
Resp.setProperty "SelectionNamespaces", XmlNamespaces
For Each transaction In Resp.getElementsByTagName("Transaction")
Debug.Print "tesst"
i = i + 1
WS.Range("A2:A200").Cells(1, i).Value = Transaction.SelectNodes("//doc2:RapidRequestId")(0).Text
Next Transaction
End With
End Sub
现在返回两行 RequestID,但它们都是相同的。理想情况下,我需要做的是在
中显示来自所有节点的数据<Transaction>
非常感谢
山姆
【问题讨论】:
-
您的实际 XML 中是否有多个事务?您发布的示例只有一个。
-
@TimWilliams 嗨,蒂姆,只有一个事务,但是根据已发出的请求数量,可能会有多个事务
标签: excel xml vba soap namespaces