从根节点检索空子节点

时间:2019-03-28 06:13:00

标签: excel vba xml-parsing

我偶然发现了从vba解析xml文件并将其存储为文本的问题。我已经从MSDN尝试了许多方法,但是无法获取空节点(即空节点)。例如,我的代码忽略了我需要的空节点。我知道它为null,但我想将节点值打印为null。

我已经在excel vba中尝试使用XMLDOM,但是无法将其打印到文件中

这是我的xml文件。

<Config>
	<Location>
		<Transmit>[0, 1000] s, precision: 10^-1 s</Transmit>
		<Time>[0, 1000] s, precision: 10^-1 s</Time>
	</Location>
	<Maximum_Speed>[0, 180] km.h^-1, precision: 10^-2 m.s^-1</Maximum_Speed>
	<Location2>
		<Time>[0, 1000] s, precision: 10^-1 s</Time>
		<Stop_Duration>[0, 1000] s, precision: 10^-1 s</Stop_Duration>
		<Authorized_Location/>
	</Location2>
</Config>

这是我的Excel VBA代码

Sub Retrive_data_from_xml()
Dim myFile As String
On Error Resume Next
myFile = "C:\Users\Subhra\Desktop\file.txt"
Open myFile For Output As #1
Dim xmlDoc As MSXML2.DOMDocument60
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.validateOnParse = False
If xmlDoc.Load("C:\Users\Subhra\Desktop\config.xml") Then
   Display_Values_with_Variables xmlDoc.ChildNodes, 0
End If
Close #1
End Sub

Function Display_Values_with_Variables(ByRef Nodes As MSXML2.IXMLDOMNodeList, ByVal spaces As Integer)
   Dim xNode As MSXML2.IXMLDOMNode
   spaces = spaces + 1
   For Each xNode In Nodes
      If xNode.NodeType = NODE_TEXT Then
         Print #1, Space(spaces) & "/" & xNode.ParentNode.nodeName & ":" & xNode.NodeValue
    End If
      If xNode.HasChildNodes Then
            Print #1, xNode.ParentNode.nodeName
            Display_Values_with_Variables xNode.ChildNodes, spaces
      End If
   Next xNode
End Function

我希望将Authorized_Location打印为 Authorized_Location:Null ,但是代码忽略了“ Authorized_Location”。我正在引用MSXML 6.0

0 个答案:

没有答案