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

标签 excel xml vba soap namespaces

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

相关文章:

mysql - jetty 配置: problems with MySQL JNDI

xml - 在实际使用中是否值得为 Restful Web 服务实现 HATEOAS?

python - 在 Python 中拆分一个大的 XML 文件

class - vba 错误 438 但对象类确实包含子

excel - 删除由变量设置的自动过滤器

vba - PowerPoint VBA 打开“修剪音频”对话框

时间:2019-03-17 标签:c#EPPAutoRowHeight

excel - 用excel生成word文档

Excel - 使用公式引用其他工作表上的值

vba - 如何使用宏代码添加 Microsoft ActiveX 数据对象 6.1 库和 Microsoft VBScript 正则表达式 5.5