【问题标题】:getElementsByTagName and undeclared namespacegetElementsByTagName 和未声明的命名空间
【发布时间】: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


【解决方案1】:

这对我有用:

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 中打印正确的值,但是今天当我测试时,它打印了两次相同的 requestID,而不是移动到下一个值。如何让这些值在 Excel 中理想地显示在每个 ID 的新行中?
  • 该代码对我有用(使用我发明的多事务 XML 示例)。如果它确实对您有用,但现在不是,我无法猜测这两次之间发生了什么变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
  • 2011-06-14
  • 2012-07-19
  • 1970-01-01
相关资源
最近更新 更多