簡體   English   中英

使用VBScript讀取多個XML

[英]Reading multiple XMLs with VBScript

我在VBScript中有以下代碼:

Dim OrganizationInfo, name
Dim Location, country

Set xmlDoc = CreateObject("Microsoft.XMLDOM") 
xmlDoc.Async = "False"
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load("C:\Users\AdminUser\XMLArchive.xml")

Set fso = CreateObject("Scripting.FileSystemObject")
Const ForAppending = 8
Set WriterObject = fso.OpenTextFile("C:\Users\AdminUser\Folder\TEXTFile.txt", ForAppending, True) 


For Each OrganizationInfo In xmlDoc.SelectNodes("//OrganizationInfo/OrganizationName")
    name = OrganizationInfo.Text

    For Each Location In OrganizationInfo.SelectNodes("//Location")
        COUNTRY = Location.Text
        WriterObject.WriteLine DATA & ";" & Pais
    Next
Next

我正在使用Xpaths在多個txt中編寫許多XML Node的Text,而沒有出現問題。

現在,我需要在插入“ XMLArchive.xml”的同一目錄中對1000個XML重復此過程,而無需知道它們的名稱...誰能幫忙嗎? 我在這里看到過類似的案例,但沒有相同的意圖。

如有疑問,請閱讀文檔 目錄中的文件可以按以下方式處理:

Set fso = CreateObject("Scripting.FileSystemObject")

Set xml = CreateObject("Msxml2.DOMDocument.6.0") 
xml.Async = False
xml.setProperty "SelectionLanguage", "XPath"

For Each f In fso.GetFolder("C:\Users\AdminUser").Files
  If LCase(fso.GetExtensionName(f)) = "xml" Then
    xml.Load f.Path

    If xml.ParseError = 0 Then
      'your XML processing code goes here
    Else
      WScript.Echo "Error parsing '" & f.Path & "': " & xml.ParseError.Reason
    End If
  End If
Next

這是一個Sub ,它將在您指定的文件夾中找到所有XML文件的名稱。

另請參見此答案,以獲取有關將完整的《 VBScript參考》下載為Windows幫助文件的說明。

Sub ProcessXmlFiles(sFolderPath)
    Dim oFso, oFolder
    Dim sFilePath

    Set oFso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFso.GetFolder(sFolderPath)
    For Each sFilePath In oFolder.Files
        If IsXmlFile(sFilePath) Then
            WScript.Echo sFilePath
            ' ProcessXmlFile(sFilePath)  ' Do your XML file processing here!
        End If
    Next

    Set oFolder = Nothing
    Set oFso = Nothing
End Sub

Function IsXmlFile(sFilePath)
    Const REGEXPR = "\.xml$"
    Dim oRegex, oMatches

    Set oRegex = New RegExp
    oRegex.Pattern = REGEXPR
    oRegex.IgnoreCase = True
    oRegex.Global = False
    Set oMatches = oRegex.Execute(sFilePath)

    IsXmlFile = (oMatches.Count > 0)
    Set oMatches = Nothing
    Set oRegex = Nothing
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM