美好的一天,
环境:Excel 2007,MSXML 6.0,Sharepoint 3
我想解析在Sharepoint列表中创建新行后返回的Sharepoint SOAP响应。但是我没有达到我想要的元素。我确信我确实忽略了一些明显的事情。
我想回复:
1)结果的ID - 这会导致问题 2)结果错误号 - 访问节点的工作 3)结果错误文本(如果存在) - 访问节点
ID 1是成功响应,ID 2是失败响应。我也想知道为什么ID 1有一个ID ID没有的结束ID标签,如果这可能会导致问题。
这是我到目前为止所做的:
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 = _
"<?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> " & _
" <UpdateListItemsResponse " & _
" xmlns=""http://schemas.microsoft.com/sharepoint/soap/""> " & _
" <UpdateListItemsResult> " & _
" <Results> " & _
" <Result ID=""1,New""> " & _
" <ErrorCode>0x00000000</ErrorCode><ID /> " & _
" <z:row ows_Title=""Direct Access Test"" " & _
" xmlns:z=""#RowsetSchema"" /> " & _
" </Result> " & _
" <Result ID=""2,New""> " & _
" <ErrorCode>0x81020014</ErrorCode> " & _
" <ErrorText>One or more field types are not installed properly. Go to the list settings page to delete these fields.</ErrorText> " & _
" </Result> " & _
" </Results> " & _
" </UpdateListItemsResult> " & _
" </UpdateListItemsResponse> " & _
" </soap:Body> " & _
"</soap:Envelope>"
'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
提前感谢您的帮助。
答案 0 :(得分:0)
同时解决问题:
要使用XPATH,所有名称空间必须获得前缀。如果在XML中缺少必须在MSXML的XML开放选项中发明(这里发明了前缀“sp”):
然后可以正确访问所有属性和子节点。
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