在 XML 文件中搜索属性值 [英] Search attribute value in XML file

查看:32
本文介绍了在 XML 文件中搜索属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to search strings in an XML file that contains a unique string

<CSVName Value="standard.csv" />

此值在standard.csv"和non-standard.csv"之间变化.

This value changes between "standard.csv" and "non-standard.csv".

我正在使用 VBScript 来搜索standard.csv"或non-standard.csv".如果它匹配standard.csv",它会回应我这是标准的",如果它匹配非标准.csv",它会回应我这是非标准的".

I am using VBScript to search "standard.csv" or "non-standard.csv". If it matches "standard.csv" it will echo me "This is standard", if it matches "non-standard.csv" it will echo me "This is non-stanadard".

这是我单击按钮时此功能的 HTA 的一部分,我不知道如何制作匹配A"或B"的正则表达式模式,然后相应地回显每个.

This is part of my HTA for this function when clicking a button, I dont know how to make pattern of reg exp for matching "A" or "B" then echo each accordingly.

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

推荐答案

为了回答直接的问题,Pattern 属性需要一个字符串,所以你必须改变这个:

To answer the immediate question, the Pattern property expects a string, so you'd have to change this:

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

进入这个:

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

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

You could even simplify the expression to this:

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

但是,显然您那里有一个 XML 文件,因此您不应该为此使用正则表达式第一名.使用实际的 XML 解析器来提取信息:

However, apparently you have an XML file there, so you shouldn't be using regular expressions for this in the first place. Use an actual XML parser to extract the information:

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 文件中搜索属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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