在XML文件中搜索属性值

时间:2015-01-28 14:56:40

标签: xml vbscript hta

我需要在包含唯一字符串的XML文件中搜索字符串

<CSVName Value="standard.csv" />

此值在&#34; standard.csv&#34;之间变化。和&#34; non-standard.csv&#34;。

我正在使用VBScript搜索&#34; standard.csv&#34;或&#34; non-standard.csv&#34;。如果匹配&#34; standard.csv&#34;它会回应我&#34;这是标准的&#34;,如果匹配&#34;非标准.sv&#34;它会回应我&#34;这是非标准的&#34;。

当点击按钮时,这是我的HTA功能的一部分,我不知道如何制作reg exp模式以匹配&#34; A&#34;或&#34; B&#34;然后相应地回应每一个。

<html>
<head>
<title></title>
<HTA:APPLICATION
  APPLICATIONNAME=""
  ID=""
  VERSION="1.0"/>
</head>

<script language="VBScript">

ub RUNCURRENTMODE
      Const ForReading = 1

      Set objRegEx = CreateObject("VBScript.RegExp")
      objRegEx.Pattern = (?:"standard.csv"|"non-standard.csv")

      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFile = objFSO.OpenTextFile("C:\aaa\settings.xml", ForReading)

      strSearchString = objFile.ReadAll
      objFile.Close

      If 
       .
       .
       .
      End If
    End Sub

</script>

<body bgcolor="buttonface">
<center>
<p><font face="verdana" color="red">CSV MODE SWITCH</font></p>
YOU ARE CURRENTLY IN STANDARD CSV MODE <p>
<input id=runbutton  class="button" type="button" value="CURRENT MODE" name="db_button"  onClick="RUCURRENTMODE" style="width: 170px"><p>
</center>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

要回答当前问题,Pattern属性需要一个字符串,因此您必须更改此内容:

objRegEx.Pattern = (?:"standard.csv"|"non-standard.csv")

进入这个:

objRegEx.Pattern = "(?:standard\.csv|non-standard\.csv)"

您甚至可以将表达式简化为:

objRegEx.Pattern = "(?:non-)?standard\.csv"

但是,显然你有一个XML文件,所以你首先要shouldn't be using regular expressions for this。使用实际的XML解析器来提取信息:

Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.async = False
xml.load "C:\aaa\settings.xml"

If xml.ParseError Then
  MsgBox xml.ParseError.Reason
  self.Close()  'or perhaps "Exit Sub"
End If

For Each n In xml.SelectNodes("//CSVName")
  Select Case n.Attributes.GetNamedItem("Value").Text
    Case "standard.csv"     : MsgBox "This is standard."
    Case "non-standard.csv" : MsgBox "This is non-standard."
    Case Else               : MsgBox "Unexpected value."
  End Select
Next