【问题标题】:Search attribute value in XML file在 XML 文件中搜索属性值
【发布时间】:2015-01-28 14:56:39
【问题描述】:

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

<CSVName Value="standard.csv" />

此值在“standard.csv”和“non-standard.csv”之间变化。

我正在使用 VBScript 搜索“standard.csv”或“non-standard.csv”。如果匹配“standard.csv”,它会回显“This is standard”,如果匹配“non-standard.csv”,它将回显“This is non-stanadard”。

这是我单击按钮时此功能的 HTA 的一部分,我不知道如何制作 reg exp 的模式以匹配“A”或“B”,然后相应地回显。

<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>

【问题讨论】:

    标签: xml vbscript hta


    【解决方案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
    

    【讨论】:

    • 感谢您的帮助。这完全符合我的要求。我将 xml 文件视为文本文件.....如果你不介意还有一个问题,我更新了我的问题中的代码,这是我的 HTA 文件中的许多按钮之一,我只希望这个按钮在里面输出结果HTA窗口没有弹出消息框,怎么办?
    • @RootLoop 请不要移动目标。如果您有新问题:将其作为新问题发布。
    • 本帖中的问题不变,我将发布一个新问题。
    猜你喜欢
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多