打开文件对话框以选择 XML 文件 [英] Open file dialog to select XML file

查看:33
本文介绍了打开文件对话框以选择 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 VBScript,它从 c: 驱动器中选择一个文件并从 XML 文件上的标记中获取信息,但我希望用户能够从对话框中选择该文件,但我似乎无法完成,这是我的脚本:

I have a VBScript that selects a file from the c: drive and gets the information from a tag on a XML file but I want the user to be able to select the file from a dialog but I cant seem to get it done, here is my script:

Dim xmlDoc, objNodeList, plot, fin

Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load("C:\Users\User\Documents\vbscript\mlb.xml")
Set objNodeList = xmlDoc.getElementsByTagName("league")


Set objFSO=CreateObject("Scripting.FileSystemObject")

' Create file
outFile= "C:\Users\User\Documents\vbscript\Leagues.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)



If objNodeList.length > 0 then
For each x in objNodeList
plot= x.getAttribute("name")

'Write to File
objFile.Write plot & vbCrLf

Next
Else
msgbox " field not found."
End If

objFile.Close

任何帮助将不胜感激.

推荐答案

对于 Windows XP,您可以使用 UserAccounts.CommonDialog 对象,请参阅文档以获取更多详细信息,但简而言之,它是这样的:

For Windows XP you can use UserAccounts.CommonDialog object, see documentation for more details but in short it's something like this:

Set dlg = CreateObject("UserAccounts.CommonDialog")
dlg.InitialDir = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
dlg.Filter = "XML files|*.xml"

If dlg.ShowOpen <> 0 Then
    Set xmlDoc = CreateObject("Msxml2.DOMDocument")
    xmlDoc.load(dlg.FileName)
    ' Your code
End If

请注意,我还使用 WScript.Shell 对象来获取用户的文档文件夹(因此您不必对其进行硬编码).我建议对输出文件也做同样的事情.

Note that I'm also using WScript.Shell object to get user's documents folder (so you don't have to hard-code it). I'd suggest to do same thing also for output file.

不幸的是,此对象已在 Windows 7 中删除(可能是因为安全问题),那么您必须使用其他东西.有很多候选人,让我们看看其中的一些.

Unfortunately this object has been removed in Windows 7 (maybe because of security issues) then you have to use something else. There are many candidates, let's see some of them.

更简单的方法是像这样使用comdlg32.dll:

Easier method is to use comdlg32.dll like this:

Set dlg = CreateObject("MSComDlg.CommonDialog.1")
dlg.InitialDir = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
dlg.Filter = "XML files|*.xml"

这个 DLL 似乎没有在每个系统上注册,如果你是这种情况,你必须 下载到 c:\windows\system32 并用 regsrv32 注册 COM 对象.

This DLL seems to don't be registered on every system, if this is your case you have to download it in c:\windows\system32 and register COM objects with regsrv32.

一个不错的选择是使用 Shell 对象(在 MSDN):

A decent alternative is to use Shell object (explore its methods on MSDN):

Set dlg = CreateObject("Shell.Application")
Set selectedFile = objShell.BrowseForFolder(0, "XML file:", &H00004000&, "C:\") 

If Not selectedFile Is Nothing Then
    Set xmlDoc = CreateObject("Msxml2.DOMDocument")
    xmlDoc.load(selectedFile.Self.Path)
End If

那不是真正的打开文件对话框,而是带有文件的浏览文件夹对话框,更好的解决方案是使用 GetOpenFileName,代码较长请参考这篇文章 获取完整来源和详细信息.简而言之,您必须导入该函数:

That isn't a true open file dialog but a browse for folder dialog with files, better solution is to use GetOpenFileName, code is longer so please refer to this article for full source and details. In short you have to import that function:

Declare Function GetOpenFileName Lib "comdlg32.dll" 
    Alias "GetOpenFileNameA" (OFN As OPENFILENAME) As Boolean

你可能还想看看GitHub上的这段代码,它处理了大部分棘手的案例(是的,这么简单的任务终于没有这么简单了!!!)

You may also want to take a look to this code on GitHub, it handles most of tricky cases (yes such simple task isn't finally so simple!!!)

这篇关于打开文件对话框以选择 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆