2016-05-16 61 views
1

私は、特定のWEB-Serviceの結果である以下のXMLを持っています。文字列配列から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>

結果として、私は「16 /月/ 2016取得しています私は上記の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 

を読むには、以下のVBAコードを持っている - 20 /月/ 2016 "でxmlresult。

私がしたいことは、すべての[string]タグの間にすべての日付値を取得することです。 どうすれば結果を得ることができますか教えてください。私はそれを配列に読み込む必要があることを理解していますが、私はどのように役立つチュートリアル(VBAとXMLの初心者)を参照するのか分かりません。

答えて

1

Webサービスからxml-documentに返されたxml-resultを読み取り、たとえば、次のように使用します。 SelectSingleNodeノードArrayOfStringを選択します。このノードには名前空間がありますので、xpathの名前空間も使用する必要があります。たとえば、すべての子ノードのテキストを読み込みます。ここにresultと宣言されたコレクションに入れてください。 HTH

注:マイクロソフトの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