2,462
社区成员
发帖
与我相关
我的任务
分享
Sub Test()
Dim xmlDoc As DOMDocument60, xmlFile As String
xmlFile = "d:\123.xml"
Set xmlDoc = New DOMDocument60
xmlDoc.async = False
xmlDoc.Load xmlFile
If xmlDoc.parseError.errorCode <> 0 Then
MsgBox "打开错误:" & xmlDoc.parseError.reason
Exit Sub
End If
Cells.Clear
Dim xmlNode As IXMLDOMNode, i As Integer
Set xmlNode = xmlDoc.documentElement
With xmlNode
Cells(1, 1) = .baseName
If .Attributes.Length > 0 Then
For i = 0 To .Attributes.Length - 1
Cells(1, i + 2) = .Attributes(i).baseName & "=" & .Attributes(i).nodeTypedValue
Next
End If
End With
GetAllNodes xmlNode, 2, 2
Set xmlNode = Nothing
Set xmlDoc = Nothing
End Sub
Sub GetAllNodes(ByVal xmlRootNode As IXMLDOMNode, iR As Long, iC As Long)
Dim xmlNode As IXMLDOMNode, i As Integer, k As Integer
For Each xmlNode In xmlRootNode.childNodes
With xmlNode
Cells(iR, iC) = .baseName
If .baseName <> "" Then
k = .Attributes.Length
If k > 0 Then
For i = 0 To k - 1
Cells(iR, iC + i + 1) = .Attributes(i).baseName & "=" & .Attributes(i).nodeTypedValue
Next
End If
iR = iR + 1
If .childNodes.Length > 0 Then GetAllNodes xmlNode, iR, iC + 1
End If
End With
Next
End Sub
在excel的vba里执行,引用xml 6.0