Excel vba 复杂的解析 XML

Excel vba Parse Complex XML

我需要从 xml 文件中获取数据并将其加载到 Excel 工作表中。我能够加载 xml 文件并在消息框中获取元素数据。这将写在 VBA for Excel 2013

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 文件的子元素还有更多,我被困在细节父元素部分,因为当它写入第二行时它为下一个 Option 祖先元素写入 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

此代码运行完美,但 need5 是可变的,大多数情况下有 2 个项目,但有时可能会有更多项目,我将如何通过在选项

中计算 need5 来实现

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

再次感谢 David G 我想通了

这是我的最终代码,也许有人可以帮助清理一下,因为我必须多次编写 need5

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