2012-02-15 75 views
1

输入一些代码后,我开始遇到错误之前,此网站正常工作。该代码不在主页上,但现在没有任何网站页面会加载。我在IIS中重新启动了该站点,但没有任何帮助。在单个页面上输入一些代码后,将无法加载经典ASP网站中的页面

这里是我输入的代码:

'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 

使用传统的ASP在Windows 7中运行IIS。用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> 
+0

实际的错误消息将有所帮助。 – 2012-02-15 21:55:35

+0

我没有收到错误消息。它从来没有加载。 – Darren 2012-02-15 21:56:12

+0

看看你的服务器日志。 – 2012-02-15 21:56:53

回答

2

让我们通过获取代码按顺序启动:

首先,我们将创造出给定父XML元素和XPath的小助手功能(可简单地说是一个子元素的tagName)将返回元素的文本值。在这种情况下,我特意choosen如果元素没有发现返回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 

最后,我们创建一个给定产品的指数将加载返回装入适当的产品细节Product类实例的GetProduct功能。

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 & """]") 
+0

非常感谢你的帖子。不幸的是,我将无法在早上检查它。 – Darren 2012-02-16 22:44:42