我正在一个项目中,使用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
但是,这在本地处理中不会返回“ nothing”,并且在我调试时会跳过循环。
经过进一步研究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>
非常感谢
山姆
答案 0 :(得分:0)
这对我有用:
Dim Resp As New DOMDocument60
Resp.setProperty "SelectionLanguage", "XPath"
Resp.LoadXML Range("C1").Value '<<< loading from worksheet for testing
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.SelectNodes("//doc2:Transaction")
Debug.Print "--------------"
Debug.Print Transaction.SelectSingleNode("doc2:RapidRequestId").Text
Debug.Print Transaction.SelectSingleNode("doc2:XRefRequestId").Text
Next Transaction