在VBscript中选择随机XML节点

在VBscript中选择随机XML节点,vbscript,xml-parsing,random-access,Vbscript,Xml Parsing,Random Access,我一直很难弄清楚如何选择随机xml节点并将其文本值存储到变量中。我将在多个实例中使用该脚本,无论XML长度如何,它都需要工作。XML文件的格式如下所示: <?xml version="1.0" encoding="utf-8" ?> <file>file path 1</file> <file>file path 2</file> <file>file path 3</file> 我知道

我一直很难弄清楚如何选择随机xml节点并将其文本值存储到变量中。我将在多个实例中使用该脚本,无论XML长度如何,它都需要工作。XML文件的格式如下所示:

<?xml version="1.0" encoding="utf-8" ?>
    <file>file path 1</file>
    <file>file path 2</file>
    <file>file path 3</file>
我知道我做错了什么,但我不知道是什么

提前感谢你们能给我的任何帮助

编辑: 第7行和第9行出现错误。预期语句结束

至于标题,我没有注意到当我在这里复制它时是这样的。在我的代码中,它是一个文件。我也在这次编辑中修复了它。但这不是我的问题。

在下一个脚本中,注释“1..6”解释了调试原始代码的步骤。可以删除所有仅调试的Wscript.Echo语句

' VB Script Document: launch under cscript
option explicit
On Error Goto 0

Dim xmlDoc, x, max, min, file, temp, xLoad

'#5 Initialize the random-number generator
' otherwise Rnd function keeps return the same only value 
randomize

Set xmlDoc = CreateObject("Microsoft.XMLDOM")

'#1 ensure loading the document
xLoad = xmlDoc.load("D:\VB_scripts\SO\files_In\29285755.xml")
Wscript.Echo "parseError.errorCode " & xmlDoc.parseError.errorCode
If xLoad Then
    '#3 invalid property assignment
    ' x=xmlDoc.getElementsByTagName("file")
    set x=xmlDoc.getElementsByTagName("file")
    ' or   '     set x = xmlDoc.documentElement.selectNodes("/title/file")
    max = x.length
    min=0

    '#6  Object required: '[object]' error in line: file = x(temp).Text
    ' i.e. index out of bounds and zero based indexing and temp >= max 
    ' temp=(Int((max-min+1)*Rnd+min))
    temp=(Int((max-min)*Rnd+min))
    Wscript.Echo "max", max, "temp", temp, "temp>=max", CStr(temp >= max)

    '#4 Object doesn't support this property or method: 'nodeText'
    ' file = x(temp).nodeText
    file = x(temp).Text
Else
    '#2 descramble and resolve error in loading the document 
    ' parseError.errorCode -1072896683
    ' parseError.reason Only one top level element is allowed in an XML document
    ' i.e. the root element missing: added <title> 
    Wscript.Echo "parseError.reason " & xmlDoc.parseError.reason
    file = "N/A"
End If
Wscript.Echo file 

1.什么是错误的东西?这个词太宽泛了,所以我们只能猜测。一些错误消息?还是意外的行为?2.我在xml文件中看不到名为title的标记;还有更多的要调试…非常感谢!错误代码帮了大忙。我发现我的XML需要一个包含文件标记的顶级标记。谢谢你让我知道了随机化和修复行file=xtemp.text把那行搞乱了。
' VB Script Document: launch under cscript
option explicit
On Error Goto 0

Dim xmlDoc, x, max, min, file, temp, xLoad

'#5 Initialize the random-number generator
' otherwise Rnd function keeps return the same only value 
randomize

Set xmlDoc = CreateObject("Microsoft.XMLDOM")

'#1 ensure loading the document
xLoad = xmlDoc.load("D:\VB_scripts\SO\files_In\29285755.xml")
Wscript.Echo "parseError.errorCode " & xmlDoc.parseError.errorCode
If xLoad Then
    '#3 invalid property assignment
    ' x=xmlDoc.getElementsByTagName("file")
    set x=xmlDoc.getElementsByTagName("file")
    ' or   '     set x = xmlDoc.documentElement.selectNodes("/title/file")
    max = x.length
    min=0

    '#6  Object required: '[object]' error in line: file = x(temp).Text
    ' i.e. index out of bounds and zero based indexing and temp >= max 
    ' temp=(Int((max-min+1)*Rnd+min))
    temp=(Int((max-min)*Rnd+min))
    Wscript.Echo "max", max, "temp", temp, "temp>=max", CStr(temp >= max)

    '#4 Object doesn't support this property or method: 'nodeText'
    ' file = x(temp).nodeText
    file = x(temp).Text
Else
    '#2 descramble and resolve error in loading the document 
    ' parseError.errorCode -1072896683
    ' parseError.reason Only one top level element is allowed in an XML document
    ' i.e. the root element missing: added <title> 
    Wscript.Echo "parseError.reason " & xmlDoc.parseError.reason
    file = "N/A"
End If
Wscript.Echo file