简体   繁体   English

如何通过UFT中的索引变量引用xml节点?

[英]How to reference an xml node by an index variable in UFT?

I have many xml files that I am trying to edit. 我有许多要编辑的xml文件。 The first node I want to edit is located at the top of all the files, and is easily changeable. 我要编辑的第一个节点位于所有文件的顶部,并且可以轻松更改。 The 2nd node is of a variable index, depending on which file I am trying to edit. 第二个节点具有可变索引,具体取决于我要编辑的文件。 I can easily locate the node, and even edit the correct node if I reference it by a number. 我可以轻松地找到该节点,甚至可以通过数字引用它来编辑正确的节点。 But, I cannot edit the node if I reference it by a counter variable. 但是,如果我通过计数器变量引用该节点,则无法编辑该节点。 Is this possible using UFT? 使用UFT是否可以? Below is what I have so far. 以下是到目前为止的内容。

'Create a blank XML document for copying and load xxx.xml file
Set xmlDoc = _
  CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("G:\xxx\xxx.xml")

'Change the sw version listed at the top of the file to 999
Set colNodes=xmlDoc.selectNodes _
  ("/Device/Model/SoftwareVersion")

For Each objNode in colNodes
   objNode.Text = "999"
Next

'Obtain the part number of the software version  
Set swNode=xmlDoc.selectNodes _
  ("/Device/Model/PartNumber")

For Each objNode in swNode
   swPartNumber = objNode.Text
Next

counter = 0

'Set up var to search through all nodes with strucutre Device/MSM/UpdateFile
Set updateFileNodes=xmlDoc.selectNodes _
  ("/Device/MassStorageMode/UpdateFile")    

'Set up var for updatefile block of major to edit later
Set majorSwNode = xmlDoc.selectNodes _
  ("/Device/MassStorageMode/UpdateFile/Version/Major")  

'Set up var for partnumber block of updatefile to search against for a math
Set partNumberNode = xmlDoc.selectNodes _
    ("/Device/MassStorageMode/UpdateFile/PartNumber")  

For Each objNode in updateFileNodes  

    For Each objNode2 in partNumberNode

        counter = counter + 1
        curPn = objNode2.Text

        isFound = swPartNumber = curPn

                If isFound = True Then 

                Dim index
                index = counter - 1

                'edit the major sw version here...  
                Set editNode = xmlDoc.selectsinglenode ("/Device/MassStorageMode/UpdateFile[counter]/Version/Major")

                editNode.Text = "9"
                xmlDoc.Save "G:\xxx\xxx.xml"                
                Exit For
                End If      
    Next    
Next

I get an error when I run this, however, if I change the line: 但是,如果我更改此行,则会在运行时收到错误消息:

Set editNode = xmlDoc.selectsinglenode ("/Device/MassStorageMode/UpdateFile[125]/Version/Major") 设置editNode = xmlDoc.selectsinglenode(“ / Device / MassStorageMode / UpdateFile [125] / Version / Major”)

using the value of my index variable in this particular case, everything performs as expected. 在这种特殊情况下,使用我的index变量的值,一切都会按预期执行。 I need to be able to replace the number with my index variable. 我需要能够用我的索引变量替换数字。

You need concatenation (&) to splice a variable's content into a string: 您需要串联(&)将变量的内容拼接为字符串:

>> counter = 125
>> s = "/Device/MassStorageMode/UpdateFile[counter]/Version/Major"
>> WScript.Echo 1, s
>> s = "/Device/MassStorageMode/UpdateFile[" & counter & "]/Version/Major"
>> WScript.Echo 2, s
>>
1 /Device/MassStorageMode/UpdateFile[counter]/Version/Major
2 /Device/MassStorageMode/UpdateFile[125]/Version/Major

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM