代码之家  ›  专栏  ›  技术社区  ›  AlexP

创建DOMDocument集合

  •  1
  • AlexP  · 技术社区  · 6 年前

    我有一个宏,用户应该在其中进行选择。xml文件在文件夹中,文件数不确定。

    在那些。xml,有两种节点类别:

    • F1作为信息节点的节点
    • 删除以前文件的一些节点(不是全部)的节点。

    一旦我加载了我的。xml文件,我必须:

    • 然后我想分析剩余的有效节点。

    我的问题是加载每个选定的文件,然后将其存储为DOMDocument。我曾考虑使用集合或数组,但这两种方法都不起作用,因为对象是通过Byref传递的,我最终得到的集合存储了最后的x时间。已加载xml。

    我(真正)想做的是:

    • 将DOMDocument byval传递给某种集合或数组进行修改

    我不能做或试图避免的事情:

    • 我无法修改。xml文件。
    • 我非常希望避免复制。xml到一个技术文件夹进行修改,因为最终,此宏将被修改为,而不是使用用户输入,解析大约200个文件夹,每个文件夹中有许多文件,并且1)这可能会严重降低性能,2)我可能在该宏的操作环境中遇到自动生成问题。

    我在stackoverflow和google上搜索了各种关键字,但似乎找不到解决方案。

    “Microsoft XML,v6.0”库已添加到我的库参考中。

    Dim xmlDoc as DOMDocument
    Dim SelFiles As FileDialogSelectedItems
    Dim nFile as long
    Dim coDocXML As New Collection
    
    Set SelFiles = InputFilesDial("xml", "*.xml", True)
    
    For nFile = 1 to SelFiles.Count
        If xmlDoc.Load(SelFiles(nFile)) Then
            coDocXML.Add Item:=xmlDoc
        End If
    Next nFile
    
    Function InputFilesDial(stDescription As String, stFilter, multiSel As Boolean, Optional stPath As String) As FileDialogSelectedItems
    
        Dim FileDial As Office.FileDialog
        If Not stPath Like vbNullString Then ChDir (stPath)
    
        Set FileDial = Application.FileDialog(msoFileDialogFilePicker)
    
        With FileDial
            .AllowMultiSelect = multiSel
            .Filters.Clear
            .Filters.Add Description:=stDescription, Extensions:=stFilter
            .Show
        End With
    
        Set InputFilesDial = FileDial.SelectedItems
    
    End Function
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Parfait    6 年前

    只需重置 xmlDoc 对象并添加 Else 无法分析的文件的处理程序。调试中的项目。打印窗口(Ctrl+G)将输出哪些文件引发了分析错误:

    Sub RunXML()
        Dim xmlDoc As DOMDocument
        Dim SelFiles As FileDialogSelectedItems
        Dim nFile As Long
        Dim coDocXML As New Collection
    
        Set SelFiles = InputFilesDial("xml", "*.xml", True)
    
        For nFile = 1 To SelFiles.Count
            Set xmlDoc = New DOMDocument             ' RESET xmlDoc OBJECT
    
            If xmlDoc.Load(SelFiles(nFile)) Then
                coDocXML.Add Item:=xmlDoc
            Else
                Debug.Print SelFiles(nFile), xmlDoc.parseError
            End If
        Next nFile
    
        Set SelFiles = Nothing
        Set xmlDoc = Nothing
    End Sub
    
        2
  •  1
  •   Parfait    6 年前

    尝试这种允许多选的方法。添加到集合后,您可以通过 xDoc1 , xDoc2 ...:

    密码

    Option Explicit
      Dim coDocXML As New Collection
    ' Declare xDoc variable as of type DOMDocument60
      Dim xDoc As MSXML2.DOMDocument60 
    
    Sub test_xmlcol()
    '  Declare a variable to contain the path of each selected item. 
    '  Even though the path is aString, the variable must be a Variant because 
    '  For Each...Next routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant
    dim cnt   As Integer
    'Declare a variable as a FileDialog object.
    Dim cnt   As Integer
    Dim fd    As FileDialog
    'Create a FileDialog object as a File Picker dialog box.
     Set fd = Application.FileDialog(msoFileDialogFilePicker)
    ' Set xDoc to memory
      Set xDoc = New MSXML2.DOMDocument60               
      xDoc.validateOnParse = False
    ' allow XPath (if DOMDocument vers. 3.0, can omit it if vers. 6.0)
      xDoc.setProperty "SelectionLanguage", "XPath"
    
    
    'Use a With...End With block to reference the FileDialog object.
    With fd
    
        'Allow the selection of multiple file.
        .AllowMultiSelect = True
        'Add a filter that includes GIF and JPEG images and make it the first item in the list.
         .Filters.Add "XML-Files", "*.xml", 1
        'Use the Show method to display the File Picker dialog box and return the user's action.
        'The user pressed the button.
        If .Show = -1 Then
    
              'Step through each string in the FileDialogSelectedItems collection
              For Each vrtSelectedItem In .SelectedItems
                cnt = cnt + 1
                'vrtSelectedItem is aString that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example displays the path in a message box.
                MsgBox "Selected item's path: " & vrtSelectedItem
                coDocXML.Add vrtSelectedItem, "xDoc" & cnt
              Next vrtSelectedItem
    
        'The user pressed Cancel.
        Else
        End If
    End With
    
    ' Have an informative look at every file    
    For cnt = 1 To coDocXML.Count
       Debug.Print cnt, "xDoc" & cnt & ": " & coDocXML.Item("xDoc" & cnt)
    Next cnt
    
    '' load some file and do something via 
    '    cnt = 1           ' only example
    '    If xDoc.Load coDocXML.Item("xDoc" & cnt) then
    ''      ... do something
    '    End If
    
    'Set the object variable to Nothing.
    Set fd = Nothing
    
    End Sub