使用VBScript遍历XML文件中的所有节点

前端之家收集整理的这篇文章主要介绍了使用VBScript遍历XML文件中的所有节点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我编写了一个VBScript,它应该遍历XML文件中的所有节点,而不管树的深度如何.除了不显示深度为2级或更高级别的节点的节点名称之外,它的效果很好.我需要节点名称和值,以便我有名称/值对供其他程序进一步处理.任何人都可以帮助我显示丢失的节点名称.

以下是我的代码

<html>
<head>
</head>
<body>

<script type="text/vbscript">
Set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("test.xml")
Dim objDocElem,strNode,strSubNode,xmlnn,xmlnv,xmlnc,xmldd,xmlfd,xmlfv

Set n_firstchild = xmldoc.documentElement.firstChild
Set p_node = n_firstchild.parentNode
Set pn_attribs = p_node.attributes
For Each pnAttr in pn_attribs
   xmlfd = xmlfd & pnAttr.name & chr(9)
   xmlfv = xmlfv & pnAttr.value & chr(9)
Next 

Set objDocElem=xmlDoc.documentElement

Set y = objDocElem.childNodes
i=0
Do While i < y.length 
If y(i).nodeType <> 3 Then
  If Isnull(y(i).childNodes(0).nodeValue) Then
     xmlnv = xmlnv & "Category" & chr(9)
  Else
     xmlnv = xmlnv & y(i).childNodes(0).nodeValue & chr(9)
  End If
  xmlnn = xmlnn & y(i).nodeName & chr(9)
  xmlnc = xmlnc + 1
  z=0
  Do While z < y(i).childNodes.length
    If y(i).childNodes(z).nodeType <> 3 Then
       xmlnc = xmlnc + 1
       xmlnn = xmlnn & y(i).childNodes(z).nodeName & chr(9)
       xmlnv = xmlnv & y(i).childNodes(z).text & chr(9)
    End If
    z=z+1
  Loop
End If
i=i+1  
Loop

document.write("form details: " & xmlfd & "<br />")
document.write("form values: " & xmlfv & "<br /><br />")
document.write("node names: " & xmlnn & "<br />")
document.write("node values: " & xmlnv & "<br />")
document.write("<br />node count: " & xmlnc & "<br />")

</script>
</body>
</html>

和XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<form penid="AJX-AAE-CRB-7P" submitted="2009-09-10 14:57:07" updated="2009-09-10 15:05:22" finalised="2009-09-10 15:59:48">
  <Forename>Russell</Forename>
  <Surname>Kilby</Surname>
  <HouseNumber>248</HouseNumber>
  <Letter>GRG</Letter>
  <Products>
    <WinSoftware>Product One</WinSoftware>
    <MacSoftware>Product Two</MacSoftware>        
    <LnxSoftware>Product Three</LnxSoftware>
    <Generic>
        <Product1>Speedo</Product1>
        <Product2>Switches</Product2>
        <Product3>BIOS</Product3>
        <TestOne>
            <Panel1>Front</Panel1>
            <Panel2>Back</Panel2>
            <Panel3>Middle</Panel3>
        </TestOne>
    </Generic>
    <Hardware>Fender</Hardware>
  </Products>
  <HouseName>Just Tea House</HouseName>
  <PostCode>B87 7DF</PostCode>
  <Insurer>ABC Cars</Insurer>
  <PolicyNumber>FDA 8D3JD7K</PolicyNumber>
  <Make>Ford</Make>
  <VehicleReg>EX51 CBA</VehicleReg>
  <DescriptionOfDamage>Big smash on the from</DescriptionOfDamage>
  <Estimate>1300</Estimate>
  <AlertManager>Yes</AlertManager>
</form>
请试试这个:
set nodes = xmlDoc.selectNodes("//*")    
for i = 0 to nodes.length
    document.write(nodes(i).nodeName & " - " & nodes(i).text & "<br />")
next
原文链接:https://www.f2er.com/xml/293090.html

猜你在找的XML相关文章