VBA Excel宏SelectSingleNode不返回任何内容

时间:2016-04-06 16:01:58

标签: excel vba macros

  

我第一次使用excel宏,我已经编写了解析一个代码的代码   Web服务响应,我想更新标签值   excel中的单个细胞。

     

以下是我的XML摘录(一个巨大的Web服务响应)

override func viewDidLoad() {
    super.viewDidLoad()
    let zurich = WALocation(name: "Zürich", id: "7287650", country: "CH", longitude: 8.53071)
    let shanghai = WALocation(name: "Shanghai", id: "1796236", country: "CN", longitude: 121.45)
    let boston = WALocation(name: "Boston", id: "4183849", country: "US", longitude: -83.78)
    let vancouver = WALocation(name: "Vancouver", id: "6173331", country: "CA", longitude: -123.11)

    addLocationControllerForLocation(shanghai)
    locationControllers[0].refreshWeatherData()
    sleep(1)
    addLocationControllerForLocation(boston)
    locationControllers[1].refreshWeatherData()
    sleep(1)
    addLocationControllerForLocation(zurich)
    locationControllers[2].refreshWeatherData()
    sleep(1)
    addLocationControllerForLocation(vancouver)
    locationControllers[3].refreshWeatherData()
}

我正在尝试获取代码<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/> <soap:Body> <bm:getTransactionResponse xmlns:bm="http://xmlns.oracle.com/XXXCLD/commerce/ZZZYYY_PPP_1"> <bm:status> <bm:success>true</bm:success> <bm:message>Wed Apr 06 09:04:32 UTC 2016 - Successfully processed API for test1</bm:message> </bm:status> <bm:transaction> <bm:category>data</bm:category> <bm:action>add</bm:action> <bm:id>1111</bm:id> <bm:process_var_name>xvgfdr</bm:process_var_name> <bm:buyer_company_name>test1</bm:buyer_company_name> <bm:supplier_company_name>test1</bm:supplier_company_name> <bm:step_var_name>waitingForInternalApproval</bm:step_var_name> <bm:last_document_number>2</bm:last_document_number> <bm:date_added>2016-04-04 12:14:57</bm:date_added> <bm:date_modified>2016-04-06 09:04:18</bm:date_modified> <bm:data_xml> <bm:transaction bm:bs_id="11111" bm:buyer_company_name="test1" bm:buyer_user_name="someone" bm:currency_pref="GBP" bm:data_type="0" bm:document_name="Transaction" bm:document_number="1" bm:document_var_name="transaction" bm:process_var_name="XXX_1" bm:supplier_company_name="test1"> <bm:_document_number>1</bm:_document_number> <bm:createdBy_t>SomeOne</bm:createdBy_t> <bm:_price_book_var_name>_default</bm:_price_book_var_name> <bm:createdDate_t>2016-04-04 00:00:00</bm:createdDate_t> <bm:currency_t>INR</bm:currency_t> <bm:_customer_t_first_name/> <bm:_customer_t_last_name/> <bm:_customer_t_company_name>Test Account</bm:_customer_t_company_name>

的值
  

以下是我一直在使用的代码。

<bm:_customer_t_company_name>
  

.SelectSingleNode(“// soap:Body”)工作正常。                    当我测试代码时,.SelectSingleNode(“// bm:_customer_t_company_name”)总是返回Nothing。

也是如此      
    

.SelectSingleNode(“// bm:getTransactionResponse”),不返回任何内容。

         

你能告诉我我做错了什么吗?

  

以下是完整XML结构enter image description here

的屏幕截图

1 个答案:

答案 0 :(得分:0)

您提供的代码没有任何问题。您应该检查Web服务实际返回的XML,以确保它具有您正在查找的标记。

我将您的示例缩减为以下VBA Sub,运行时没有错误。

Sub Test()
    Dim xml
    Set xml = CreateObject("MSXML2.DOMDocument.6.0")
    xml.LoadXML "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <SOAP-ENV:Header xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/""/> <soap:Body> <bm:getTransactionResponse xmlns:bm=""http://xmlns.oracle.com/XXXCLD/commerce/ZZZYYY_PPP_1""> <bm:status> <bm:success>true</bm:success> <bm:message>Wed Apr 06 09:04:32 UTC 2016 - Successfully processed API for test1</bm:message> </bm:status> <bm:transaction> <bm:category>data</bm:category> <bm:action>add</bm:action> <bm:id>1111</bm:id> <bm:process_var_name>xvgfdr</bm:process_var_name> <bm:buyer_company_name>test1</bm:buyer_company_name> <bm:supplier_company_name>test1</bm:supplier_company_name> <bm:step_var_name>waitingForInternalApproval</bm:step_var_name> <bm:last_document_number>2</bm:last_document_number> <bm:date_added>2016-04-04 12:14:57</bm:date_added> <bm:date_modified>2016-04-06 09:04:18</bm:date_modified> <bm:data_xml> <bm:transaction bm:bs_id=""11111"" bm:buyer_company_name=""test1"" bm:buyer_user_name=""someone"" bm:currency_pref=""GBP"" bm:data_type=""0"" bm:document_name=""Transaction"" bm:document_number=""1"" bm:document_var_name=""transaction"" bm:process_var_name=""XXX_1"" bm:supplier_company_name=""test1""> <bm:_document_number>1</bm:_document_number> <bm:createdBy_t>SomeOne</bm:createdBy_t> <bm:_price_book_var_name>_default</bm:_price_book_var_name> <bm:createdDate_t>2016-04-04 00:00:00</bm:createdDate_t> <bm:currency_t>INR</bm:currency_t> <bm:_customer_t_first_name/> <bm:_customer_t_last_name/> <bm:_customer_t_company_name>Test Account</bm:_customer_t_company_name> </bm:transaction> </bm:data_xml> </bm:transaction> </bm:getTransactionResponse> </soap:Body> </soap:Envelope>"

    xml.SetProperty "SelectionNamespaces", "xmlns:bm=""http://xmlns.oracle.com/XXXCLD/commerce/ZZZYYY_PPP_1"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"""

    Debug.Print xml.SelectSingleNode("//bm:_customer_t_company_name").Text
End Sub

输出为Test Account