在单个页面上输入一些代码后,经典 ASP 站点中的任何页面都不会加载

发布于 2025-01-06 04:21:01 字数 2510 浏览 0 评论 0原文

在我输入一些代码后开始遇到错误之前,该网站运行正常。该代码不在主页上,但现在不会加载任何网站页面。我在 IIS 中重新启动了该站点,但这没有帮助。

以下是我输入的代码:

'Prepare to parse XML
Set objXML = Server.CreateObject(Microsoft.XMLDOM)

'Set Asynchoronous = false
objXML.async = False

'Load the XML file.
'User Server.MapPath method is the XML is located in your site.
'Else you can use the absolute path.
objXML.Load (Server.MapPath(Products.xml))

'If there is any errors pasring the file the notify
If objXML.parseError.errorCode = 0 Then        
    'Response.Write(objXML.parseError.reason)
Else objXML.parseError.errorCode <> 0 Then
    'Get ALL the Elements by the tag name product
    Set products = objXML.getElementsByTagName(product)

    Select Case iItemID
        Case 1
            aParameters = Array(products.item(0).childNodes(0).text, products.item(i).childNodes(2).text, products.item(i).childNodes(2).text)
        Case 2
            aParameters = Array(products.item(1).childNodes(0).text, products.item(i).childNodes(2).text, products.item(i).childNodes(2).text)        
    End Select

    ' Return array containing product info.
    GetItemParameters = aParameters
End If

Running IIS in Windows 7 using classic ASP。使用 Notepad++ 进行编辑。

这是 XML 文件:

<configuration>
<products>
    <product>   
        <image>
            <![CDATA[ /Images/Atlas Gloves.jpg ]]>
        </image>    
        <name>
            <![CDATA[ Atlas Nitrile Touch Gloves ]]>
        </name>
        <description>
            <![CDATA[ Atlas Nitrile Touch is available in 6 vibrant colors, and is America’s #1 glove for the Lawn and Garden market. Atlas gloves have a breathable nylon back and are machine washable. Like a “second skin,” these gloves are the most comfortable! Atlas Nitrile gloves are the #1 gardening gloves. Atlas Nitrile gloves act like a "second skin" between the user and their work, offering full dexterity and grip. Atlas Nitrile Gloves are perfect for gardening, but their uses expand to so many places – the woodshop, the workshop, the workplace. ]]>
        </description>
        <size>
            <![CDATA[ Small, Medium ]]>
        </size>
        <color>
            <![CDATA[ Purple, Pink, Green, Orange ]]>
        </color>
    </product>
</products>
</configuration>

This site was working properly before I started encountering an error after entering some code. The code was not on the home page but none of the site pages will load now. I restarted the site in IIS and that did not help.

Here is the code that I entered:

'Prepare to parse XML
Set objXML = Server.CreateObject(Microsoft.XMLDOM)

'Set Asynchoronous = false
objXML.async = False

'Load the XML file.
'User Server.MapPath method is the XML is located in your site.
'Else you can use the absolute path.
objXML.Load (Server.MapPath(Products.xml))

'If there is any errors pasring the file the notify
If objXML.parseError.errorCode = 0 Then        
    'Response.Write(objXML.parseError.reason)
Else objXML.parseError.errorCode <> 0 Then
    'Get ALL the Elements by the tag name product
    Set products = objXML.getElementsByTagName(product)

    Select Case iItemID
        Case 1
            aParameters = Array(products.item(0).childNodes(0).text, products.item(i).childNodes(2).text, products.item(i).childNodes(2).text)
        Case 2
            aParameters = Array(products.item(1).childNodes(0).text, products.item(i).childNodes(2).text, products.item(i).childNodes(2).text)        
    End Select

    ' Return array containing product info.
    GetItemParameters = aParameters
End If

Running IIS in Windows 7 using classic ASP. Editing with Notepad++.

Here is the XML file:

<configuration>
<products>
    <product>   
        <image>
            <![CDATA[ /Images/Atlas Gloves.jpg ]]>
        </image>    
        <name>
            <![CDATA[ Atlas Nitrile Touch Gloves ]]>
        </name>
        <description>
            <![CDATA[ Atlas Nitrile Touch is available in 6 vibrant colors, and is America’s #1 glove for the Lawn and Garden market. Atlas gloves have a breathable nylon back and are machine washable. Like a “second skin,” these gloves are the most comfortable! Atlas Nitrile gloves are the #1 gardening gloves. Atlas Nitrile gloves act like a "second skin" between the user and their work, offering full dexterity and grip. Atlas Nitrile Gloves are perfect for gardening, but their uses expand to so many places – the woodshop, the workshop, the workplace. ]]>
        </description>
        <size>
            <![CDATA[ Small, Medium ]]>
        </size>
        <color>
            <![CDATA[ Purple, Pink, Green, Orange ]]>
        </color>
    </product>
</products>
</configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2025-01-13 04:21:01

让我们首先按顺序获取代码:

首先,我们将创建一个小辅助函数,它给定一个父 XML 元素和一个 XPath(可以只是子元素的 tagName),该函数将返回元素的文本值。在本例中,如果未找到该元素,我特意选择返回 null,但如果您愿意,也可以将返回值保留为空:

Function GetElemText(parentElem, path)
     Dim elem: Set elem = parentElem.selectSingleNode(path)
     If Not elem Is Nothing Then
         GetElemText = elem.text
     Else
         GetElemText = null
     End If
End Function

现在我们将创建一个小型 VBScript 类,其中每个产品元素都有一个字段。此类有一个 LoadFromXml 方法,给定产品 xml 元素将提取字段值。

Class Product

    Public Image
    Public Name
    Public Description
    Public Size
    Public Color

    Public Sub LoadFromXml(prodElem)
         Image = GetElemText(prodElem, "image")
         Name = GetElemText(prodElem, "name")
         Description = GetElemText(prodElem, "description")
         Size = GetElemText(prodElem, "size")
         Color = GetElemText(prodElem, "color")
    End Sub

End Class

最后,我们创建一个 GetProduct 函数,给定产品索引,将加载返回一个 Product 类实例,其中加载了适当的产品详细信息。

Function GetProduct(productIndex)

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

    objXML.async = False 
    objXML.setProperty "SelectionLanguage", "XPath"
    objXML.Load Server.MapPath("Products.xml") ''# Assumes Products xml in same folder as this script 

    Dim elem: Set elem = objXML.documentElement.selectSingleNode("products/product[" & productIndex & "]") 
    If Not elem Is Nothing Then
        Set GetProduct = new Product
        GetProduct.LoadFromXml elem
    Else
        Set GetProduct = Nothing
    End If

End Function

请注意,使用命名元素消除了对“幻数”的需要,“幻数”的值要么必须记住,要么放在常量中,并且非常脆弱。还使用 XPath 作为选择语言和更具体的 ProgID。总而言之,更加强大,并且在这种情况下也可以工作。

如果您的产品 xml 在应用程序的生命周期内保持相当静态,请考虑以下变化:

Function GetProduct(productIndex)

    Dim objXML
    If IsEmpty(Application.Contents("Products")) Then
        Set objXML = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0") 
        objXML.async = False 
        objXML.setProperty "SelectionLanguage", "XPath"
        Set Application.Contents("Products") = objXML
    Else
        Set objXML = Application.Contents("Products")
    End If

    objXML.Load Server.MapPath("Products.xml") ''# Assumes Products xml in same folder as this script 

    Dim elem: Set elem = objXML.documentElement.selectSingleNode("products/product[" & productIndex & "]") 
    If Not elem Is Nothing Then
        Set GetProduct = new Product
        GetProduct.LoadFromXml elem
    Else
        Set GetProduct = Nothing
    End If

End Function

这会将 XML DOM 加载到应用程序商店中,从而节省每次需要产品时重新加载的成本。

我建议的另一项更改是,为了检索产品元素而依赖了解产品元素的顺序位置是非常脆弱的。考虑向产品元素添加 id="1" 属性。然后可以通过以下方式检索它:

    Dim elem: Set elem = objXML.documentElement.selectSingleNode("products/product[@id=""" & productIndex & """]")

Lets start by getting the code in order:

First we'll create a little helper function which given a parent XML element and an XPath (can be simply a tagName of a child element) will return the text value of an element. In this case I have deliberately choosen to return null if the element isn't found but you could leave the return value empty if you prefer:

Function GetElemText(parentElem, path)
     Dim elem: Set elem = parentElem.selectSingleNode(path)
     If Not elem Is Nothing Then
         GetElemText = elem.text
     Else
         GetElemText = null
     End If
End Function

Now we'll create a little VBScript class which has a field for each of the product elements. This class has a LoadFromXml method which given an product xml element will extract the field values.

Class Product

    Public Image
    Public Name
    Public Description
    Public Size
    Public Color

    Public Sub LoadFromXml(prodElem)
         Image = GetElemText(prodElem, "image")
         Name = GetElemText(prodElem, "name")
         Description = GetElemText(prodElem, "description")
         Size = GetElemText(prodElem, "size")
         Color = GetElemText(prodElem, "color")
    End Sub

End Class

Finally we create a GetProduct function that given the index of a product will load return a Product class instance loaded with the appropriate product details.

Function GetProduct(productIndex)

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

    objXML.async = False 
    objXML.setProperty "SelectionLanguage", "XPath"
    objXML.Load Server.MapPath("Products.xml") ''# Assumes Products xml in same folder as this script 

    Dim elem: Set elem = objXML.documentElement.selectSingleNode("products/product[" & productIndex & "]") 
    If Not elem Is Nothing Then
        Set GetProduct = new Product
        GetProduct.LoadFromXml elem
    Else
        Set GetProduct = Nothing
    End If

End Function

Note the use named elements eliminates the need for "magic numbers" the values of which you would either have to remember or place in constants and are very fragile. Also the use of XPath as the selection language and a more specific ProgID. All in all much more robust and in this case also working.

If your products xml remains fairly static over the life time of the application consider this variation of:

Function GetProduct(productIndex)

    Dim objXML
    If IsEmpty(Application.Contents("Products")) Then
        Set objXML = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0") 
        objXML.async = False 
        objXML.setProperty "SelectionLanguage", "XPath"
        Set Application.Contents("Products") = objXML
    Else
        Set objXML = Application.Contents("Products")
    End If

    objXML.Load Server.MapPath("Products.xml") ''# Assumes Products xml in same folder as this script 

    Dim elem: Set elem = objXML.documentElement.selectSingleNode("products/product[" & productIndex & "]") 
    If Not elem Is Nothing Then
        Set GetProduct = new Product
        GetProduct.LoadFromXml elem
    Else
        Set GetProduct = Nothing
    End If

End Function

This loads the XML DOM into the application store saving the cost of reloading every time a product is needed.

One other change I would recommend, the reliance of know the ordinal position of a product element in order to retrieve it is quite fragile. Consider adding an id="1" attribute to the product element. It can then be retrieved with:

    Dim elem: Set elem = objXML.documentElement.selectSingleNode("products/product[@id=""" & productIndex & """]")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文