2017-05-13 3 views
0

xmlファイルからデータを取得し、Excelワークシートにロードする必要があります。私はxmlファイルを読み込んで、メッセージボックスに要素データを取得することができます。これは、Excel 2013Excel vba Parse Complex XML

<?xml version="1.0" encoding="utf-8" ?> 
<Response > 
    <Status>10</Status> 
    <callMessage>Success.</callMessage> 
    <PullR version="1"> 
     <responseStatus>1000</responseStatus> 
     <responseMessage>Success.</responseMessage> 
     <Options> 
      <Option> 
       <header> 
        <need1>text1</need1> ---- "COLUMN1" 
        <need2> 
         <![CDATA[text2]]> 
        </need2> ---- "COLUMN2" 
        <need 3>this text3 together with need 2</need3>"COLUMN2" 
        <need4> 
         <![CDATA[Text4]]> 
        </need4> ---"COLUMN4" 
       </header> 
       <Details> - ""this one could be 1 to 10 child element (detail) this sample has 2 "" 
        <detail> 
         <need5>text5</need5> " COLUMN5" 
         <need6>date6</need6> 
         <need7>time7</need7> 
         <need8> 
          <![CDATA[text8]]> 
         </need8> 
         </detail> 
        <detail> ---- ""this should be on second row in excel sheet "" 
         <need5>text5</need5> --- "COLUMN5 ROW +1" 
         <need6>date6</need6> ---- "COLUMN6 ROW +1" 
         <need7>time7</need7> 
         <need8> 
          <![CDATA[text8]]> 
         </need8> 
        </detail> 
       </Details> 
      </Option> 
     </Options> 
    </PullR> 
</Response> 

私はxmlファイルの子要素を減らすxmlファイル構造のためのVBAで書かれてますが、より多くの、私はそれを書き込むときのように、詳細親要素の部分にこだわっています2番目の行は、次のオプション祖先要素にneed1を書き込みます。 セル内の日付は、xmlファイルにあるように正確に入力したテキスト(原子値)フィールドになります。


Dim Options As IXMLDOMNodeList 
Dim Option As IXMLDOMNode 

Set Options = rosDoc.SelectNodes("/Response/PullR/Options/Option") 

    For Each Option In Options 

    MsgBox Trip.SelectNodes("header/need1").Item(0).Text 
    MsgBox Trip.SelectNodes("Details/Detail/need5").Item(0).Text 
    MsgBox Trip.SelectNodes("Details/Detail/need5").Item(1).Text 
    Next 

このコードは完璧に動作しますが、要求5は、2つの項目がある変数ほとんどの場合ですが、時にはそれ以上はどのように私はオプションの中に要求5オフのカウントを行うことにより、そこに行くことがあります

+0

あなたがいなくなった場所にコードを追加してください。 –

+0

は、XMLをクエリする方が簡単かもしれません。https://support.office.com/en-us/article/Connect-to-an-XML-file-Power-Query-78bbe5b5-ebf2-4036-bdcb-72ea47f34ee3、または['FILTERXML'関数](https://support.office.com/en-us/article/FILTERXML-function-4df72efc-11ec-4951-86f5-c1374812f5b7) – Slai

+0

私はAPIからxmlファイルを取得することを忘れていましたが、その部分は私がやった –

答えて

0

VBAがxpathをサポートしています。これは、XMLファイルからデータを処理する最も簡単な方法です。ここで

は、あなたが始めるだろういくつかのサンプルコードです:

Dim doc As DOMDocument 
Set doc = New DOMDocument 
doc.Load "C:\x.xml" 
Dim Variables As IXMLDOMNodeList 
Dim variable As IXMLDOMNode 
Set Variables = doc.SelectNodes("/Environment/Variable") 
    For Each variable In Variables 
     Debug.Print variable.SelectNodes("Caption").Item(0).Text 
     Debug.Print variable.SelectNodes("Type").Item(0).Text 
    Next 
0

おかげで再びデビッド・GIが、これは私の最終的なコード多分誰かが私が持っているので、少しのクリーンアップに役立つ可能性だった

それを考え出しました必要数を何回か書きます。

Dim Options As IXMLDOMNodeList 
Dim Option As IXMLDOMNode 

Set Options = rosDoc.SelectNodes("/Response/PullR/Options/Option") 

    For Each Option In Options 

    MsgBox Trip.SelectNodes("header/need1").Item(0).Text 
    MsgBox Trip.SelectNodes("Details/Detail/need5").Item(0).Text 

    IF not Trip.SelectNodes("Details/Detail/need5").Item(1) is nothing then 
    MsgBox Trip.SelectNodes("Details/Detail/need5").Item(1).Text 

    IF not Trip.SelectNodes("Details/Detail/need5").Item(2) is nothing then 
    MsgBox Trip.SelectNodes("Details/Detail/need5").Item(2).Text 

    'etc........ 

    Next