ASP 经典 - XML Dom

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/892161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:30:26  来源:igfitidea点击:

ASP Classic - XML Dom

xmlasp-classicvbscript

提问by Chris McKee

I've got the unpleasurable task of working on a Classic ASP site (VBSCRIPT) and need to parse out the following information in a loop.

我在经典 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>

I'm trying to grab the product refs and then the images (4th main node down)

我正在尝试获取产品参考,然后获取图像(向下的第 4 个主节点)

I've been faffing with this for a while now and am suffering brain block after not using ASP for over 2 years.

我一直在玩这个一段时间,并且在不使用 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

%>

Any help would be great before I lose my mind to ASP

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

--- Additional ---

- - 额外的 - -

Using this provides a full output as I'd hope its the node attributes I cant seem to grab.

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

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


Final Code To Dump the XML (Cerebrus Below)

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


%>


Working XPATH Version (modified from Pete Duncanson Below)

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


%>


Anthony Jones points out that its better to be specific so you may want to change

Anthony Jones 指出最好是具体的,所以你可能想要改变

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

to

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

Which still works with the final code.

这仍然适用于最终代码。

采纳答案by Cerebrus

Yeah, having to work in classic ASP occasionally transports me back to the Stone age too... I feel your pain!

是的,不得不在经典的 ASP 中工作偶尔也会让我回到石器时代......我感受到你的痛苦!

IIRC, in your second code snippet, you just need to add :

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

回答by Pete Duncanson

Switch to using xpath instead and it will be much easier.

改用 xpath 会容易得多。

Dim nodes
nodes = objXML.selectNodes( "//products" )

Dim images

For each node in nodes
    Response.Write( "<ul>" )
    Response.Write( "<li>Ref: " + node.selectNodes( "@ref" ).Text + "</li>" )
    images = node.selectNodes( "images/image" )
    For each image in images
        Response.Write( "<li>Image: " + image.selectNodes( "@ref" ).Text + "</li>" )
    Next
    Response.Write( "</ul>" )
Next

I'm a JScript ASP coder, like you not done VBScript for an age so the above "might" need a bit of polish (I had to strip out all the ";" at the end of the all the lines, such is the habit of adding them) but should point you in the right direction at least.

我是一个 JScript ASP 编码员,就像你很久没有做过 VBScript 一样,所以上面的“可能”需要一点修饰(我不得不去掉所有行末尾的所有“;”,这就是添加它们的习惯)但至少应该为您指明正确的方向。

Hope that helps.

希望有帮助。

回答by Vikram

Try the following command to get the attribute value specifically for the image node:

尝试以下命令来获取专门针对图像节点的属性值:

node.Attributes.getNamedItem("ref").Text