简体   繁体   English

使用VBScript读取多个XML

[英]Reading multiple XMLs with VBScript

I've got the following piece of code in 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

I'm using Xpaths to write lots of XML Node's Text in multiple txts, without problems. 我正在使用Xpaths在多个txt中编写许多XML Node的Text,而没有出现问题。

Now I need to repeat this process with 1000 XMLs in the same directory that "XMLArchive.xml" is inserted, without knowing their names... Can anyone please help? 现在,我需要在插入“ XMLArchive.xml”的同一目录中对1000个XML重复此过程,而无需知道它们的名称...谁能帮忙吗? I've seen similar cases here, but none with the same intention. 我在这里看到过类似的案例,但没有相同的意图。

When in doubt, read the documentation . 如有疑问,请阅读文档 Files in a directory can be processed like this: 目录中的文件可以按以下方式处理:

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

Here's a Sub that will find the names of all of the XML files in a folder that you designate. 这是一个Sub ,它将在您指定的文件夹中找到所有XML文件的名称。

Also see this answer for instructions on downloading a complete VBScript Reference as a Windows help file. 另请参见此答案,以获取有关将完整的《 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