ASP Classic – XML Dom

前端之家收集整理的这篇文章主要介绍了ASP Classic – XML Dom前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个令人不愉快的任务是在一个经典ASP站点(VBSCRIPT)上工作,需要在一个循环中解析出以下信息.
<xml>
  <product ref="xxx">
    <xxx/>
    <xxx/>
    <xxx/>
    <images>
      <image ref="JCCCCCC" />
      <image ref="JCCCCCD" />
    </images>
  </product>
  <product ref="xxx">
    <xxx/>
    <xxx/>
    <xxx/>
    <images>
      <image ref="JCCCCCC" />
      <image ref="JCCCCCD" />
    </images>
  </product>
</xml>

我正试图抓住产品参考然后图像(第4个主节点向下)

我已经有一段时间对此感到不满,并且在不使用ASP超过2年后遭受脑阻塞.

<%
 Set objXML = Server.CreateObject("Microsoft.XMLDOM")
 Set objLst = Server.CreateObject("Microsoft.XMLDOM")
 Set objHdl = Server.CreateObject("Microsoft.XMLDOM")

 objXML.async = False
 objXML.Load (Server.MapPath("\") & "\xmlupdate\product.xml")

 If objXML.parseError.errorCode <> 0 Then
     'handle the error
 End If

 Set objLst = objXML.getElementsByTagName("Product")
 SizeofObject = objLst.length-1
 response.Write(SizeofObject&"<br><br>")

 For i = 0 To (SizeofObject-1)

    Set objHnd = objLst.item(i)
    Response.Write(objHdl.childNodes(0).text)
 Next

%>

在我对ASP失去理智之前,任何帮助都会很棒

—附加—

使用它提供了一个完整的输出,因为我希望它的节点属性似乎无法抓住.

<%
Set objLst = objXML.getElementsByTagName("Product")
SizeofObject = objLst.length-1
response.Write(SizeofObject&"<br><br>")


For each elem in objLst
    set childNodes = elem.childNodes
    for each node in childNodes
        Response.Write node.nodeName & "  =  " & node.text & "<br />" & vbCrLf
    next
    Response.Write "<hr>" & vbCrLf
Next
%>

最终代码转储XML(下面的Cerebrus)

<%
Set objLst = objXML.getElementsByTagName("Product")
SizeofObject = objLst.length-1
response.Write(SizeofObject&"<br><br>")


For each elem in objLst
    set childNodes = elem.childNodes
    for each node in childNodes

        Response.Write node.nodeName & "  =  " & node.text & "<br />" & vbCrLf
        If lcase(node.nodeName)="images" then 
            Response.Write("<B>Images Hit</B></br>")
            set xattchildnodes = node.childNodes
            For Each attchildnodes in xattchildnodes
                For Each att in attchildnodes.Attributes
                    Response.Write att.Name & "  =  " & att.text & "<br />" & vbCrLf
                Next
            Next
        End If
    next
    Response.Write "<hr>" & vbCrLf
Next


%>

使用XPATH版本(从下面的Pete Duncanson修改)

<%
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.Load (Server.MapPath("\") & "\Product.xml")
'etc'

Dim nodes
set nodes = objXML.selectNodes("//xml/Product")

Dim images

For each node in nodes
    Response.Write("<ul>")
    Response.Write("<li>Ref: " & node.getAttribute("ref") & "</li>")
    Set images = node.selectNodes("Images/Image")
    For each image in images
       Response.Write( "<li>Image:"& image.getAttribute("ref") &"</li>" )
    Next
    Response.Write( "</ul>" )
Next


%>

安东尼琼斯指出,更好的具体,所以你可能想要改变

Set objXML = Server.CreateObject("Microsoft.XMLDOM")

Set objXML = Server.CreateObject("MSXML2.DOMDocument.3.0")

哪个仍适用于最终代码.

解决方法

是的,不得不经常使用经典的ASP,让我回到石器时代……我感受到你的痛苦!

IIRC,在您的第二个代码段中,您只需添加

for each node in childNodes
  Response.Write node.nodeName & "  =  " & node.text & "<br />" & vbCrLf
  '***Add the following:
  For Each att in node.Attributes
    Response.Write att.Name & "  =  " & att.text & "<br />" & vbCrLf
  Next
next
原文链接:https://www.f2er.com/aspnet/251358.html

猜你在找的asp.Net相关文章