gpt4 book ai didi

excel - getElementsByTagName 和未声明的命名空间

转载 作者:行者123 更新时间:2023-12-04 17:06:17 24 4
gpt4 key购买 nike

我正在开发一个使用 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>

非常感谢

山姆

最佳答案

这对我有用:

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

关于excel - getElementsByTagName 和未声明的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56178994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com