热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

每个循环到for循环的vbscript-vbscriptforeachlooptoforloop

HiIhavewrittenaprograminVBScript,nowIwanttoreplaceForEachloopwithaForloop.你好,我用V

Hi I have written a program in Vbscript, now I want to replace For Each loop with a For loop.

你好,我用Vbscript写了一个程序,现在我想用For循环替换每个循环。

For Each node In objMSXML.selectNodes(sXPath)                   
    Set li = document.createElement("li")
    li.innerText = i & " " & node.text
    ul.appendChild li
    i = i +1
Next

I have not been able to figure it out as I also need to know the number of nodes returned from Xpath.

我还没算出来,因为我还需要知道Xpath返回的节点数。

2 个解决方案

#1


2  

The node list returned by .SelectNodes() is a collection with a .length property that can be traversed using a zero-based integer index:

.SelectNodes()返回的节点列表是一个具有.length属性的集合,可以使用从零开始的整数索引遍历该集合:

Option Explicit

Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..\data\33921005.xml")
Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument.6.0")
oXML.load sFSpec
If 0 = oXML.parseError Then
   Dim ndlName : Set ndlName = oXML.selectNodes("/Envelope/Body/Request/individual/hasName/*")
   Dim ndName
   For Each ndName In ndlName
       WScript.Echo ndName.tagName
   Next
   Dim iNd
   For iNd = 0 To ndlName.length - 1
       WScript.Echo iNd, ndlName(iNd).tagName
   Next
Else
   WScript.Echo oXML.parseError.reason
End If

output:

输出:

cscript 36053711.vbs
firstName
lastName
0 firstName
1 lastName

#2


3  

Following on from my initial comment.

根据我最初的评论。

The selectNodes() method returns a IXMLDOMNodeList object which is a NodeList collection.

selectNodes()方法返回一个IXMLDOMNodeList对象,它是一个NodeList集合。

As with any collection it contains members common to collection objects

与任何集合一样,它包含集合对象共有的成员

Properties

属性

  • length - Number of Nodes in the collection
  • 长度-集合中节点的数量

Methods

方法

  • item - Access to individual Nodes within the collection
  • 项目-对集合中各个节点的访问

For full list of member properties and methods see IXMLDOMNodeList Members

有关成员属性和方法的完整列表,请参阅IXMLDOMNodeList成员

There should be no logical reason why you need to change your For Each to a For loop as you can just use the length property to retrieve the number of Nodes in the collection.

应该没有逻辑理由需要将For Each改为For循环,因为您可以使用length属性来检索集合中的节点数量。

Option Explicit

Dim node, nodes, li, i, sXPath, NumberOfNodes
Set nodes = objMSXML.selectNodes(sXPath)
'Retrieve number of Nodes
NumberOfNodes = nodes.length
For Each node In nodes                   
    Set li = document.createElement("li")
    li.innerText = i & " " & node.text
    ul.appendChild li
    i = i +1
Next

Useful Links

  • Document Object Model (Core) Level 1
  • 文档对象模型(核心)级别1
  • MSDN - IXMLDOMNodeList
  • MSDN——IXMLDOMNodeList

推荐阅读
author-avatar
加肥的猫miao_115
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有