3

我需要在 Excel 中的 VBA 中打开一个 XML 文件。我在 XML 文件中查找的两个数据字符串是一个标签。这些字符串可以在多个标签下找到,但我还需要将它们连接起来。

<Systems>
  <conveyor ConveyorNumber="132000">
    <conveyor>132000</conveyor>
    <productName>IQ</productName>
  </conveyor>
</Systems>

那里有更多数据,但我只需要

<conveyor>132000</conveyor>  

&

<productName>IQ</productName>

. 有多个

<Systems></Systems>

在文件中,所以我需要对文件中的系统进行计数,将所需的两个字符串连接起来,并将它们全部放在 Excel 工作表中的一列中。有没有办法做到这一点?

4

1 回答 1

4

尝试下面的代码来解析 XML 文件

Sub parseXML()

    Dim strPath As String
    strPath = Application.GetOpenFilename

    Dim XDoc As Object
    Set XDoc = CreateObject("MSXML2.DOMDocument")
    XDoc.async = False
    XDoc.validateOnParse = False
    XDoc.Load (strPath)
    Set xObjDetails = XDoc.ChildNodes(0)
    Set xObject = xObjDetails.FirstChild
    For Each xObject In xObjDetails.ChildNodes
        MsgBox "Child nodes count " & xObjDetails.ChildNodes.Length
        For Each xChild In xObject.ChildNodes
            MsgBox xChild.BaseName & " " & xChild.Text
        Next xChild
    Next xObject

End Sub
于 2013-11-01T15:53:18.897 回答