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

发布于 2024-08-15 12:36:25 字数 3261 浏览 10 评论 0原文

我编写了一个 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>

I have written a VBScript which is supposed to tranverse all nodes in an XML file, irrespective of the depth of the tree. This it does well except that the node names for the those nodes which are 2 or more levels deep are not displayed. I need the node names as well as the values so that I have name/value pairs for further processing by other programs. Can anyone please help me in getting the missing node names displayed.

Below is my code:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雪碎 2024-08-22 12:36:25

请尝试这个:

set nodes = xmlDoc.selectNodes("//*")    
for i = 0 to nodes.length
    document.write(nodes(i).nodeName & " - " & nodes(i).text & "<br />")
next

Please, try this:

set nodes = xmlDoc.selectNodes("//*")    
for i = 0 to nodes.length
    document.write(nodes(i).nodeName & " - " & nodes(i).text & "<br />")
next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文