我正在努力让 excel 解析一个 xml 文件。我找到了很多例子,但似乎没有一个是我正在寻找的,而且我似乎无法克服错误“对象变量或未设置块变量”
xml 格式正确,如下所示:
<xml tag>
<PLMXML>
<WorkflowTemplate name="">
<argument name="">
</argument>
</WorkflowTemplate >
<WorkflowTemplate name="">
等等
我正在尝试使用 VBA 来单独获取所有儿童名称的值并获取参数的名称。我一直收到此代码的错误:
Dim xmlDoc As MSXML2.DOMDocument
Dim xmlElement As MSXML2.IXMLDOMElement
Dim xmlNode As MSXML2.IXMLDOMNode
Dim xmlAttribute As MSXML2.IXMLDOMAttribute
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.async = False
xmlDoc.validateOnParse = False
'ENTER THE PATH WHERE THE XML Workflow DOCUMENT IS STORED:
Dim DocumentPath As String
DocumentPath = InputBox("Enter the full path for the xml workflow document, example: C:\workflows\workflowseasy.xml", "Workflow XML File path", "C:\workflows\workflowseasy.xml")
xmlDoc.Load (DocumentPath)
Set xmlElement = xmlDoc.DocumentElement
Set xmlNode = xmlElement.SelectSingleNode("WorkflowTemplate[0]")
Set xmlAtribute = xmlNode.Attributes.getNamedItem("name")
我不清楚如何在 excel vba 中使用此解析器获取文档中的数据。任何帮助将不胜感激。我目前在参考文献中选择了 Microsoft XML,v6.0。
更新
我一直在深入研究它并提出了以下代码,尽管我仍然遇到同样的错误:
Dim xmlDoc As MSXML2.DOMDocument60
Dim xmlRoot As MSXML2.IXMLDOMNode
Dim xmlTemplate As MSXML2.IXMLDOMNode
Dim xmlAttributes As MSXML2.IXMLDOMNamedNodeMap
Dim xmlName As MSXML2.IXMLDOMNode
Dim xmlChildren As MSXML2.IXMLDOMNodeList
Dim xmlChild As MSXML2.IXMLDOMNode
Dim intI As Long
intI = 1
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False
xmlDoc.validateOnParse = False
'ENTER THE PATH WHERE THE XML Workflow DOCUMENT IS STORED:
Dim DocumentPath As String
DocumentPath = InputBox("Enter the full path for the xml workflow document, example: C:\workflows\workflowseasy.xml", "Workflow XML File path", "C:\workflows\workflowseasy.xml")
xmlDoc.Load (DocumentPath)
Set xmlRoot = xmlDoc.DocumentElement *****these say they are empty when debugging
Set xmlChildren = xmlRoot.ChildNodes *****these say they are empty when debugging
For Each xmlTemplate In xmlChildren *****error occures here
If xmlTemplate.nodeName = "WorkflowTemplate" Then
Set xmlAttributes = xmlTemplate.Attributes
Set xmlName = xmlAttributes.getNamedItem("name")
ActiveSheet.Cells(int1, 1).Value = xmlName.Text
Set xmlChildren = xmlTemplate.ChildNodes
intI = intI + 1
End If
Next xmlTemplate
最后更新* *
弄清楚了。文件的加载是问题所在。由于某种原因,从 msg 框中传递字符串不起作用,但从 gui 文件选择器传递它可以。这是我最终使用的代码。
Dim xmlDoc As MSXML2.DOMDocument60
Dim xmlRoot As MSXML2.IXMLDOMNode
Dim xmlTemplate As MSXML2.IXMLDOMNode
Dim xmlAttributes As MSXML2.IXMLDOMNamedNodeMap
Dim xmlName As MSXML2.IXMLDOMNode
Dim xmlChildren As MSXML2.IXMLDOMNodeList
Dim xmlChild As MSXML2.IXMLDOMNode
Dim intI As Long
intI = 1
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False
xmlDoc.validateOnParse = False
'ENTER THE PATH WHERE THE XML Workflow DOCUMENT IS STORED:
Dim DocumentPath As String
With Application.FileDialog(msoFileDialogOpen)
.Title = "Choose File"
.AllowMultiSelect = False
.Show
'DocumentPath.Show
DocumentPath = .SelectedItems(1)
End With
xmlDoc.Load (DocumentPath)
Set xmlRoot = xmlDoc.DocumentElement
Set xmlChildren = xmlRoot.ChildNodes
For Each xmlTemplate In xmlChildren
If xmlTemplate.nodeName = "WorkflowTemplate" Then
Set xmlAttributes = xmlTemplate.Attributes
Set xmlName = xmlAttributes.getNamedItem("name")
ActiveSheet.Cells(int1, 1).Value = xmlName.Text
Set xmlChildren = xmlTemplate.ChildNodes
intI = intI + 1
End If
Next xmlTemplate
目前,代码在分配值部分中断,但通过代码,变量正在提取正确的值并正确提取 xml 信息。