热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

解析SOAPXML结果(VBA/MSXML6)-ParseSOAPXMLResult(VBA/MSXML6)

Goodday,Environment:Excel2007,MSXML6.0,Sharepoint3环境:Excel2007,MSXML6.0,Sharepoint3I

Good day,

Environment: Excel 2007, MSXML 6.0, Sharepoint 3

环境:Excel 2007,MSXML 6.0,Sharepoint 3

I would like to parse a Sharepoint SOAP response which is returned after creating new rows in a Sharepoint list. I do however not get to the elements I wish to. I am sure I do overlook something obvious.

我想解析在Sharepoint列表中创建新行后返回的Sharepoint SOAP响应。但是,我没有达到我想要的元素。我相信我确实忽略了一些明显的东西。

I would like to return:

我想回复:

1) The ID of the result - this is causing issues 2) Result Error Number - works accessing the node 3) Result Error Text (if existing) - works accessing the node

1)结果的ID - 这导致问题2)结果错误号 - 访问节点的工作3)结果错误文本(如果存在) - 工作访问节点

ID 1 is a success response, ID 2 is a failure response. I am also wondering why ID 1 has a closing ID tag which ID 2 has not and if this may causes problems.

ID 1是成功响应,ID 2是失败响应。我也想知道为什么ID 1有一个ID ID没有的结束ID标签,如果这可能导致问题。

This is what I have so far:

这是我到目前为止:

Sub Parse_Soap_Response()

Dim xml_soap_response As String

Dim xml_document As Object 'New MSXML2.DOMDocument60
Dim xml_nodes_collection As Variant 'IXMLDOMSelection
Dim xml_node_element As Variant 'IXMLDOMElement
Dim xml_node_attributes As Variant 'IXMLDOMNamedNodeMap
Dim xml_node_attribute As Variant 'IXMLDOMAttribute

Set xml_document = CreateObject("MSXML2.DOMDocument.6.0")

'Set XML opening options
With xml_document
  .Async = False
  .PreserveWhiteSpace = False
  .ValidateOnParse= False
  .ResolveExternals = False
  'Use full XPath functionality
  .SetProperty "SelectionLanguage", "XPath"
  'Add specific Namespaces to work with Paths
  .SetProperty "SelectionNamespaces", "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"" " & _
                                      "xmlns=""http://schemas.microsoft.com/sharepoint/soap/"" " & _
                                      "xmlns:rs=""urn:schemas-microsoft-com:rowset"" " & _
                                      "xmlns:z=""#RowsetSchema"""

End With



xml_soap_respOnse= _
" " & _
"  " & _
"      " & _
"          " & _
"              " & _
"                  " & _
"                      " & _
"                        0x00000000  " & _
"                          " & _
"                     " & _
"                     " & _
"                        0x81020014 " & _
"                        One or more field types are not installed properly. Go to the list settings page to delete these fields. " & _
"                     " & _
"                 " & _
"             " & _
"         " & _
"     " & _
""

'Load XML File and report Error if any
If Not xml_document.LoadXML(xml_soap_response) Then

    MsgBox "Error while loading XML File:" & vbCrLf & vbCrLf & _
      "Line Number " & xml_document.parseError.Line & vbCrLf & _
      xml_document.parseError.reason & " (" & xml_document.parseError.ErrorCode & ")", vbCritical, "Error"

    Exit Sub

End If


Set xml_nodes_collection = xml_document.SelectNodes("//soap:Envelope/soap:Body/UpdateListItemsResponse/UpdateListItemsResult/Results")

'Go through all nodes
For Each xml_node_element In xml_nodes_collection

    'Go through all attributes
    For Each xml_node_attribute In xml_node_element.attributes

      'Should return
      '1) The ID of the result
      '2) Result Error Number
      '3) Result Error Text
      Debug.Print xml_node_attribute.nodename & "=" & xml_node_attribute.NodeValue

    Next

Next


'Close the xml document
Set xml_document = Nothing


End Sub

Thanks in advance for any help.

在此先感谢您的帮助。

1 个解决方案

#1


0  

Issue resolved meanwhile:

问题同时解决:

To work with XPATH, all namespaces must get a prefix. If missing in the XML one must be invented within the XML opening options of MSXML (here prefix "sp" is invented):

要使用XPATH,所有名称空间必须获得前缀。如果在XML中缺少必须在MSXML的XML开放选项中发明(这里发明了前缀“sp”):

Then all attributes and child nodes can be accessed properly.

然后可以正确访问所有属性和子节点。

xml_document.SetProperty "SelectionNamespaces", "xmlns:sp=""http://schemas.microsoft.com/sharepoint/soap/""

'Set the start path
Set xml_nodes_collection = xml_document.SelectNodes("//soap:Envelope/soap:Body/sp:UpdateListItemsResponse/sp:UpdateListItemsResult/sp:Results/sp:Result")

'Go through all row IDs
For Each xml_node_element In xml_nodes_collection
   row_id = xml_node_element.attributes(0).NodeValue

   For Each xml_child_node In xml_node_element.ChildNodes
       '.......
   Next
Next

推荐阅读
author-avatar
baiwei001
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有