【发布时间】:2021-12-25 12:51:02
【问题描述】:
我在引用加载的 XML 文件中使用的架构文件时遇到问题。架构文件位于folder="C:\Users\zander\Desktop\20211112094529\Recipe\RECIPEs\Data\Sequences\",因为 XML 文件位于多个子文件夹中,我不想将架构文件复制到每个子文件夹中。
我找到的所有示例都与http... 位置有关,谁能帮我参考本地文件夹?
<?xml version="1.0"?>
<SEQUENCE Format="2.0" Name="hardware\bbx" xmlns="x-schema:sequence_schema.xml">
<PASSPORT Frozen="No" Type="Production" AccessModifyGroups="All" AccessDisplayGroups="All"
Version="1.0" ReviseTime="28/11/2018 11:36:13.384" Revisor="Wesley" CreateTime="11/01/2012
17:38:14.765" Creator="Wes" Description="">
<CUSTOM>
<CUSTOM_PARAM Name="SendLotEndSignal" Value="0"/>
</CUSTOM>
</PASSPORT>
<STEPS>
<STEP SeqEndCleanRecipe="NO_RECIPE" Recipe="STANDARD\205C" Chambers="B2B35" SectionName="Steps">
<CUSTOM>
<CUSTOM_PARAM Name="Type" Value="PAAP"/>
<CUSTOM_PARAM Name="Block" Value="B2"/>
</CUSTOM>
</STEP>
<STEP SeqEndCleanRecipe="NO_RECIPE" Recipe="STANDARD\110C" Chambers="B2B14" SectionName="Steps">
<CUSTOM>
<CUSTOM_PARAM Name="Type" Value="PAHP"/>
<CUSTOM_PARAM Name="Block" Value="B2"/>
</CUSTOM>
</STEP>
</STEPS>
</SEQUENCE>
这是我正在使用的代码(已更新为全新代码)。当我将架构文件复制到与 XML 相同的文件夹时,代码可以完美运行,它可以正常工作,但我不想这样做,因为我有很多子文件夹:
Option Explicit
'Sub ReadAllXML(Ffolder As String)
Sub ReadAllXML()
Dim Ffolder As String
Dim FilePath, XMLFileName, RecipeID As String
Dim main_folder As String
Dim oXMLFile, Recipe As Object
Dim Col, NumberOfElements, LastRow As Long
Dim n As Object
Dim RecipeName, ChamberName, UnitKind, BlockName As String
Dim strtxt, Fromtxt, FromPos, PathWithoutSequences, FirstFolder As String
Dim wks As Worksheet
'main folder is the folder where the schema file is stored
main_folder = "C:\Users\zander\Desktop\20211112094529\Recipe\RECIPEs\Data\Sequences\"
'Ffolder are different folders where all the XML files are stored
Ffolder = "C:\Users\zander\Desktop\20211112094529\Recipe\RECIPEs\Data\Sequences\Subfolder1\subfolder2\"
Set oXMLFile = CreateObject("Microsoft.XMLDOM")
Set wks = ThisWorkbook.Worksheets("Sheet1")
XMLFileName = Dir(Ffolder & "*.xml")
If XMLFileName <> "" Then
FilePath = Ffolder & XMLFileName
' Namespace = "xmlns=x-schema:sequence_schema.xml" "xsi:schemaLocation=""file:///C:\Users\zander\Desktop\20211112094529\Recipe\RECIPEs\Data\Sequences\sequence_schema.xml""
oXMLFile.Load FilePath
'oXMLFile.SetProperty "SelectionLanguage", "XPath"
'oXMLFile.SetProperty "SelectionNamespaces", "xmlns=x-schema:sequence_schema.xml"
'oXMLFile.resolveExternals = False
'oXMLFile.validateOnParse = False
Debug.Print oXMLFile.parseError.ErrorCode & " " & oXMLFile.parseError.reason & " " & oXMLFile.parseError.Line
'Set elements = oXMLFile.getElementsByTagName("STEP")
'RecipeName = oXMLFile.SelectSingleNode("//STEP").Attributes.getNamedItem("Recipe").Text
'BlockName = oXMLFile.SelectSingleNode("//STEP/CUSTOM").Attributes.getNamedItem("CUSTOM_PARAM").Text
Col = 10
NumberOfElements = oXMLFile.getElementsByTagName("STEP").Length
With wks
LastRow = wks.Cells(wks.Rows.Count, "C").End(xlUp).Row + 1
strtxt = FilePath
Fromtxt = "Sequences"
'Totxt = "test"
FromPos = InStr(strtxt, Fromtxt)
'ToPos = InStr(strtxt, Totxt)
'extract the text string between the two words
'ExtractStr = Mid(strtxt, FromPos + Len(Fromtxt), ToPos - FromPos - Len(Fromtxt))
PathWithoutSequences = Right(strtxt, Len(strtxt) - FromPos - 9)
FirstFolder = Left(PathWithoutSequences, Len(PathWithoutSequences) - Len(XMLFileName) - 1)
.Cells(LastRow, 3) = FirstFolder
.Cells(LastRow, 8) = XMLFileName
For Each n In oXMLFile.SelectNodes("//STEP")
RecipeName = n.Attributes.getNamedItem("Recipe").Text
ChamberName = n.Attributes.getNamedItem("Chambers").Text
UnitKind = n.ChildNodes(0).ChildNodes(0).Attributes.getNamedItem("Value").Text
BlockName = n.ChildNodes(0).ChildNodes(1).Attributes.getNamedItem("Value").Text
.Cells(LastRow, Col) = RecipeName
.Cells(LastRow, Col + 1) = ChamberName
.Cells(LastRow, Col + 2) = UnitKind
.Cells(LastRow, Col + 3) = BlockName
Col = Col + 4
Next
End With
End If
End Sub
【问题讨论】:
-
你得到什么错误? XML 解析器是否抱怨缺少“sequence_schema.xml”文件?您是否将它与 XML 放在同一个文件夹中?您可以尝试 oXMLFile.resolveExternals = false oXMLFile.validateOnParse = false。
-
是的,sequence_schema.xml 文件与 XML 本身不在同一个文件夹中,因此它仅在我将架构文件复制到同一个文件夹时才有效,但我想避免这种情况。我尝试添加 2 个额外的 cmets,但它不起作用。
-
Debug.Print XMLFileNameOVL在即时窗口中返回什么,Dir之后? -
它返回XML文件名,结合它可以打开的文件夹路径。我使用
Dir,因为最终它将遍历文件夹中的所有 XML 文件
标签: xml vba xpath xsd xml-namespaces