VBA读取具有相似名称的XML节点

时间:2014-08-11 06:48:06

标签: vba xml-parsing

我有一个看起来像

的XML
    <view>
<fieldlist>
<viewField>
<formatterParameterMap>
<entry>

<string>    </string>
<string     </string>

</entry>
</formatterParameterMap>
</viewField>
</fieldlist>
<view>

我当前的代码只能读取第一个字符串信息(文本) 但是我需要读取所有节点。

我当前的代码

'list data source information
Private Sub CommandButton1_Click()
Dim Init As Integer
Dim xmlDoc As New DOMDocument
Dim DomNode  As IXMLDOMNode
Init = 7


Sheets("Views").Select
XML_Path = Cells(3, "F").Value
'Set xmlDoc = CreateObject("MSXML2.DOMDocument")

'MsgBox XML_Path

xmlDoc.Load ("C:\DRIVE\BMC\DSS\01_DSS_NExT\SP_1\IN APP\Automation_YF\Total_YF_XML.xml")
child_count = 0

For Each DomNode In xmlDoc.getElementsByTagName("entry")                      'Itrate through the SubViewList
ActiveSheet.Cells(Init, 1) = DomNode.SelectNodes("string")(0).Text
'ActiveSheet.Cells(Init, 2) = DomNode.SelectNodes("entry")(0).Text

child_count = child_count + 1

Init = Init + 1
Next DomNode
Dim node As IXMLDOMNode
Dim childNode As IXMLDOMNode

End Sub

我正在努力让这个工作。任何帮助都会得到诚实的赞赏

1 个答案:

答案 0 :(得分:1)

为了在第二级使用getElementsByTagName,需要将DomNode声明为IXMLDOMElement,而不是IXMLDOMNode。

另外,你的xml是狡猾的 - 你的第二个视图标签应该关闭,而你的一个字符串标签缺少一个&#34;&gt;&#34; - 在代码下面看到正确形成的样本XML ...

enter image description here

Private Sub CommandButton1_Click()
Dim Init As Integer
Dim xmlDoc As New DOMDocument
Dim DomNode  As IXMLDOMElement
Dim childNode As IXMLDOMNode
Init = 7


Sheets("Views").Select
'XML_Path = Cells(3, "F").Value
'Set xmlDoc = CreateObject("MSXML2.DOMDocument")

'MsgBox XML_Path

xmlDoc.Load ("C:\DRIVE\BMC\DSS\01_DSS_NExT\SP_1\IN APP\Automation_YF\Total_YF_XML.xml")

'Assemble the qualified node list and walk it
For Each DomNode In xmlDoc.getElementsByTagName("entry")

' Assemble the qualified node list, walk it and output their text
  For Each childNode In DomNode.getElementsByTagName("string")
    ActiveSheet.Cells(Init, 1).Value2 = childNode.Text
    Init = Init + 1
  Next childNode

Next DomNode

End Sub

编辑:OP在SelectNodes上使用IXMLDOMNode的替代解决方案。

Private Sub CommandButton1_Click()
Dim Init As Integer
Dim xmlDoc As New DOMDocument
Dim DomNode  As IXMLDOMNode
Dim childNode As IXMLDOMNode
Init = 7


Sheets("Views").Select
'XML_Path = Cells(3, "F").Value
'Set xmlDoc = CreateObject("MSXML2.DOMDocument")

'MsgBox XML_Path

xmlDoc.Load ("C:\DRIVE\BMC\DSS\01_DSS_NExT\SP_1\IN APP\Automation_YF\Total_YF_XML.xml")

'Assemble the qualified node list and walk it
For Each DomNode In xmlDoc.getElementsByTagName("entry")

' Assemble the qualified node list, walk it and output their text
  For Each childNode In DomNode.SelectNodes("string")
    ActiveSheet.Cells(Init, 1).Value2 = childNode.Text
    Init = Init + 1
  Next childNode

Next DomNode

End Sub

适当的XML样本......

<?xml version="1.0" encoding="utf-8"?>
<view>
    <fieldlist>
        <viewField>
            <formatterParameterMap>
                <entry>    
                    <string>8888</string>
                    <string>99999</string>
                </entry>
            </formatterParameterMap>
        </viewField>
    </fieldlist>
    <fieldlist>
        <viewField>
            <formatterParameterMap>
                <entry>
                    <string>22222222</string>
                    <string>2323232323</string>
                </entry>
            </formatterParameterMap>
        </viewField>
    </fieldlist>
    <fieldlist>
        <viewField>
            <formatterParameterMap>
                <entry>
                    <string>36363636</string>
                    <string>3737373737</string>
                </entry>
            </formatterParameterMap>
        </viewField>
    </fieldlist>
    <fieldlist>
        <viewField>
            <formatterParameterMap>
                <entry>
                    <string>50505050</string>
                    <string>5151515151</string>
                </entry>
            </formatterParameterMap>
        </viewField>
    </fieldlist>
    <fieldlist>
        <viewField>
            <formatterParameterMap>
                <entry>
                    <string>64646464</string>
                    <string>6565656565</string>
                </entry>
            </formatterParameterMap>
        </viewField>
    </fieldlist>
    <fieldlist>
        <viewField>
            <formatterParameterMap>
                <entry>
                    <string>78787878</string>
                    <string>7979797979</string>
                </entry>
            </formatterParameterMap>
        </viewField>
    </fieldlist>
</view>