加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP Classic – XML Dom

发布时间:2020-12-16 00:03:58 所属栏目:asp.Net 来源:网络整理
导读:我有一个令人不愉快的任务是在一个经典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 im
我有一个令人不愉快的任务是在一个经典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("") & "xmlupdateproduct.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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读