2016-05-16 64 views
1

我有以下的XML是来自某个WEB服务的结果。阅读字符串阵列FOM XML到VBA

<?xml version="1.0" encoding="UTF-8"?> 
 

 
-<ArrayOfString xmlns="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
 

 
<string>16/May/2016 - 20/May/2016</string> 
 

 
<string>20/May/2016 - 23/May/2016</string> 
 

 
<string>23/May/2016 - 27/May/2016</string> 
 

 
<string>27/May/2016 - 30/May/2016</string> 
 
</ArrayOfString>

我有以下VBA代码读取上面的XML

strRet = PostWebservice(strUrlEBenefit, strSoapAction, strXml) 
    intPos1 = InStr(strRet, "<string>") + 8 
    intPos2 = InStr(strRet, "</string>") 

    If intPos1 > 11 And intPos2 > 0 Then 
     xmlresult = Mid(strRet, intPos1, intPos2 - intPos1) 
    End If 

,结果我得到“16 /月/ 2016 - 20 /月/ 2016 “在xmlresult中。

我想要做的是获取所有[字符串]标签之间的所有日期值。 你能指导我怎样才能达到结果?我知道我需要将它读入数组,但我不知道如何以及没有看到任何有用的教程(VBA和XML初学者)参考。

回答

1

将从Web服务返回的xml结果读取到xml文档中并使用它们。 SelectSingleNode选择节点ArrayOfString。该节点具有namaspaces,因此您还需要在xpath中使用命名空间。然后,只需阅读所有子节点文本,例如到这里宣布为result的集合中。 HTH

注:添加引用到Microsoft XML,v6.0的DLL

Sub GetDateValues() 
    Dim xmlDocument As MSXML2.DOMDocument60 
    Dim xmlNamespaces As String 
    Dim arrayOfStringNode As IXMLDOMNode 
    Dim result As Collection 
    Dim xmlNode As IXMLDOMNode 
    Dim strRet As String 

    strRet = PostWebservice(strUrlEBenefit, strSoapAction, strXml) 
    Set xmlDocument = New DOMDocument60 

    If Not xmlDocument.LoadXML(strRet) Then 
     Err.Raise xmlDocument.parseError.ErrorCode, , xmlDocument.parseError.reason 
    End If 

    xmlNamespaces = "xmlns:myns='http://tempuri.org/'" 
    xmlDocument.setProperty "SelectionNamespaces", xmlNamespaces 
    Set arrayOfStringNode = xmlDocument.SelectSingleNode("/myns:ArrayOfString") 
    Set result = New Collection 

    For Each xmlNode In arrayOfStringNode.ChildNodes 
     result.Add xmlNode.Text 
    Next xmlNode 
End Sub