Xml 使用ASP/VB获取节点属性值

Xml 使用ASP/VB获取节点属性值,xml,dom,asp-classic,vbscript,Xml,Dom,Asp Classic,Vbscript,我有以下XML模式: <lyrics> <response code="0">SUCCESS</response> <searchResults> <result id="120745" hid="gXTQx5ywE0M=" exactMatch="true"> <title>Opera Singer</title>

我有以下XML模式:

 <lyrics>
      <response code="0">SUCCESS</response>
      <searchResults>
           <result id="120745" hid="gXTQx5ywE0M=" exactMatch="true">
                <title>Opera Singer</title>
                <artist>
                     <name>Cake</name>
                </artist>
           </result>
      </searchResults>
 </lyrics>

成功
歌剧演员
蛋糕
使用VB,如何获得
exactMatch
的值?我尝试过许多不同的方法,但似乎都不管用。有什么想法吗?

试试这个:

Set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
    xmldoc.async = true
    xmldoc.Load Server.MapPath("yourfile.xml")

'' // query for a specific result        
Set result = xmldoc.SelectSingleNode("//result[@id='120745']")
Response.Write(result.GetAttribute("exactMatch") & "<br />")

'' // get all results elements
Set results = xmldoc.SelectNodes("//result")
For Each result In results
    Response.Write(result.GetAttribute("exactMatch") & "<br />")
Next
Set xmldoc=Server.CreateObject(“Microsoft.XMLDOM”)
xmldoc.async=true
xmldoc.Load Server.MapPath(“yourfile.xml”)
''//查询特定结果
Set result=xmldoc.SelectSingleNode(//result[@id='120745']))
Response.Write(result.GetAttribute(“exactMatch”)和“
”) ''//获取所有结果元素 Set results=xmldoc.SelectNodes(//result) 对于结果中的每个结果 Response.Write(result.GetAttribute(“exactMatch”)和“
”) 下一个
使用以下代码:-

<%
Option Explicit
Dim doc: Set doc = YourFunctionThatFetchesTheResults()

Dim result
For Each result in doc.SelectNodes("/lyrics/searchresults/result")
   RenderResult result
Next

Sub RenderResult(result)
   Dim ID : ID = result.getAttribute("ID")
   Dim exactMatch : ID = result.getAttribute("extactMatch")
   Dim title : title = GetElemText(result,"title")
   Dim artist : artist = GetElemText(result, "artist/name")
   %>
   <tr><td><%=ID%></td><td><%=exactMatch%></td><td><%=title%></td><td><%=artist ></td></tr>
   <%
End Sub

Function GetElemText(node, path)
    Dim elem : Set elem = node.selectSingleNode(path)
    If Not elem is Nothing Then
        GetElemText = elem.Text
    End If
End Function
顺便说一句,避免被“//”的便利性所诱惑,如果文档结构已知,那么显式导航该结构是一种更健壮的方法

Dim result
For Each result in doc.SelectNodes("/lyrics/searchresults/result[@extactMatch='true']")
   RenderResult result
Next