访问 XML 文件中的特定属性

Access specific attribute inside XML file

我正在尝试获取此 XML 文件特定属性中的文本。比如描述里面的文字,类型等等

目前我收到一个错误:

Line: 23
Error: Object required: 'objNode.attributes.getNameItem(....)'

如何访问加载 XML 文件的特定属性?描述,详细信息->服务名称,类型

<!DOCTYPE html>
<html lang="en">
<head>
    <title>XML</title>
    <HTA:APPLICATION
        APPLICATIONNAME = "XPOS removal tool"
    />
</head>
<script language="VBScript">
    Sub Window_onLoad
        Set xmlDoc = CreateObject("Microsoft.XMLDOM")
        xmlDoc.Async = "False"
        xmlDoc.Load("programs1.xml")

        strQuery = "/steps/step"

        Set colNodes = xmlDoc.selectNodes( strQuery )

        htmlString = "<table><tr><th>Description</th><th>Type</th><th>Status</th></tr>"

        For Each objNode in colNodes
            htmlString = htmlString & "<tr><td>"& objNode.attributes.getNamedItem("description").value &"</td><td>test</td></tr>"
            // htmlString = htmlString & "<tr><td>"& objNode.text &"</td><td>test</td></tr>"
        Next

        htmlString = htmlString & "</table>"

        DataArea.innerHTML = htmlString
    End Sub
</script>

<body>
<div id="DataArea"></div>
</body>
</html>

XML数据:

<?xml version='1.0'?> 
<steps>
<step>
    <description>Description 1</description>
    <type>Type 5</type>
    <details>
      <runFolder>c:\windows</runFolder>
      <runFile>v3-x86.exe</runFile>
    </details>
  </step>
  <step>
    <description>Description 2</description>
    <type>Type 4</type>
    <details>
      <serviceName>COMRedirector</serviceName>
      <processName>COMRedirectorServ</processName>
    </details>
  </step>
  <step>
    <description>Description 3</description>
    <type>Type 3</type>
    <details>
      <serviceName>OSUpdate</serviceName>
      <processName>OSUpdateServ</processName>
    </details>
  </step>
</steps>

您通过 .GetAttribute(...) 而不是 .Attributes.GetNamedItem(...) 获得属性。然而,在取消隐藏并查看您的实际 XML 数据之后:您首先不是在寻找属性。您想要 select XML 个节点的 text/value:

For Each objNode in xmlDoc.selectNodes("//description")
    htmlString = htmlString & "<tr><td>" & objNode.text & _
                 "</td><td>test</td></tr>"
Next

基本上这就是我想做的。

    Set xmlDoc = CreateObject("Microsoft.XMLDOM")
    xmlDoc.Async = "False"
    xmlDoc.Load("programs.xml")

    strQuery = "/steps/step"

    Set colNodes = xmlDoc.selectNodes( strQuery )

    For Each objNode in colNodes
        strDescription = objNode.SelectSingleNode("description").text
    Next