VBScript修改元素(很多,具有相同的元素名称,但具有不同的属性)? [英] VBScript to modify an Element (of many, with same Element names but with different Attributes)?

查看:274
本文介绍了VBScript修改元素(很多,具有相同的元素名称,但具有不同的属性)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个简单的VBScript来修改现有的XML文件。我能够编写一个VBScript来修改一个元素,但我目前遇到的问题是,我有多个元素具有相同的元素/标记名称,但不同的属性,如下面的XML文档示例显示:

 < MyDoc> 
< Section name =First>
< ...... />
< / Section>
< Section name =Second>
< ......>
< Parameter name =Servicevalue =MsrNdp.dll/>
< / Section>
< / MyDoc>假设我只想将参数Service的值更改为LdrXMP。 dll(然后保存):由于存在多个名为Section的元素,我将如何指定我正在接触元素Section,其属性值=Second??



到目前为止,我有以下简单的VBScript代码:如何调整我的代码以获得我想要的?感谢您的帮助。

pre $
$ Create $($ Microsoft $ XMLDOM
xmlDoc .Async =False
xmlDoc.Load(C:\Users\Frank\Desktop\MyDoc.xml)
设置colNodes = xmlDoc.selectNodes _
( / MyDoc / Section)
对于colNodes中的每个objNode
objNode.Text =LdrXMP.dll
下一个
xmlDoc.SaveC:\Users\Frank\\ \\桌面\MyDoc.xml


解决方案

搜索表达式,返回name属性,selectSingleNode()和getAttributeNode(),如下所示:

  Dim oFS:Set oFS = CreateObject(Scripting.FileSystemObject)
Dim sFSpec:sFSpec = goFS.GetAbsolutePathName(.. \testdata\xml\so14541579.xml)
Dim objMSXML:Set objMSXML = CreateObject( Msxml2.DOMDocument.6.0)
objMSXML.setPropertySelectionLanguage,XPath
objMSXML.async = False

objM SXML.load sFSpec

如果0 = objMSXML.parseError然后
Dim sXPath:sXPath =/ MyDoc / Section [@name =Second] / Parameter
Dim ndFnd:Set ndFnd = objMSXML.selectSingleNode(sXPath)
如果ndFnd是Nothing那么
WScript.Echo sXPath,找不到
其他
WScript.Echo ndFnd.getAttributeNode value)。value
ndFnd.getAttributeNode(value)。value =abracadabra.dll
WScript.Echo objMSXML.xml
End If
else
WScript.Echo objMSXML.parseError.reason
结束如果

输出:

  MsrNdp.dll 
< MyDoc>
< Section name =First>
< Parameter name =Servicevalue =MsrNdp.dll/>
< / Section>
< Section name =Second>
< Parameter name =Servicevalue =abracadabra.dll/>
< / Section>
< / MyDoc>


I need to write a simple VBScript to modify an existing XML file. I am able to write a VBScript to modify an element, but the problem I am currently encountering is that I have multiple elements with the same Element/tag names but different ATTRIBUTES as the below XML document sample shows:

<MyDoc>
   <Section name="First">
      <....../>
  </Section>
  <Section name ="Second">
     <......>
     <Parameter name="Service" value="MsrNdp.dll"/>
   </Section>
 </MyDoc>

Let's assume I wanted to change ONLY the "value" of the parameter "Service" to "LdrXMP.dll" (then save it): Since there is more than one Element called "Section", how would I specify that I am reffering to the Element "Section" whose attribute value = "Second" ??

I have the following simple VBScript code so far: How can I tweak my code below to get what I want? Thanks for your help.

Set xmlDoc = _
  CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("C:\Users\Frank\Desktop\MyDoc.xml")
Set colNodes=xmlDoc.selectNodes _
("/MyDoc/Section ")
For Each objNode in colNodes
   objNode.Text = "LdrXMP.dll"
Next
xmlDoc.Save "C:\Users\Frank\Desktop\MyDoc.xml"

解决方案

Use an XPath search expression that referes to the name attribute, selectSingleNode(), and getAttributeNode() as in:

  Dim oFS      : Set oFS      = CreateObject("Scripting.FileSystemObject")
  Dim sFSpec   : sFSpec       = goFS.GetAbsolutePathName("..\testdata\xml\so14541579.xml")
  Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument.6.0")
  objMSXML.setProperty "SelectionLanguage", "XPath"
  objMSXML.async = False

  objMSXML.load sFSpec

  If 0 = objMSXML.parseError Then
     Dim sXPath : sXPath    = "/MyDoc/Section[@name=""Second""]/Parameter"
     Dim ndFnd  : Set ndFnd = objMSXML.selectSingleNode(sXPath)
     If ndFnd Is Nothing Then
        WScript.Echo sXPath, "not found"
     Else
        WScript.Echo ndFnd.getAttributeNode("value").value
        ndFnd.getAttributeNode("value").value = "abracadabra.dll"
        WScript.Echo objMSXML.xml
     End If
  Else
     WScript.Echo objMSXML.parseError.reason
  End If

output:

MsrNdp.dll
<MyDoc>
        <Section name="First">
                <Parameter name="Service" value="MsrNdp.dll"/>
        </Section>
        <Section name="Second">
                <Parameter name="Service" value="abracadabra.dll"/>
        </Section>
</MyDoc>

这篇关于VBScript修改元素(很多,具有相同的元素名称,但具有不同的属性)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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