2012-02-15 42 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