2010-12-22 54 views
0

Morning All,用經典的ASP顯示XML數據

我很努力讓這段代碼正常工作。我已經花了數小時調整,改變它..它仍然不會工作。

<% 
Dim nTitle, nDescription, nWeight, nImage, nPrice, StoreMemberID, nDobaID, StoreItemCatID, nTrue, nFalse, MarkupPerc, nYes, nNo 
Dim nFileName, intNewID, paryProducts 

FUNCTION FetchProductRecords(str) 

    Dim objHTTP, RSSURL, RSSFeed, xmlRSSFeed, RSSItems, objItem, objChild, paryOut, nCell 
    Redim paryOut(6,-1) 
    nCell = 0 
    RSSURL = "http://www.storeboard.com/excel/" & str 

    Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP") 
    objHTTP.open "GET",RSSURL,false 
    objHTTP.send 
    RSSFeed = objHTTP.responseText 

    Set xmlRSSFeed = Server.CreateObject("MSXML2.DomDocument") 
    xmlRSSFeed.async = false 
    xmlRSSFeed.LoadXml(RSSFeed) 
    Set objHTTP = Nothing 

    Set RSSItems = xmlRSSFeed.getElementsByTagName("Row") 
    Set xmlRSSFeed = Nothing 

    Redim preserve paryOut(6,RSSItems.length-1) 
    FOR i = 0 TO RSSItems.length - 1 
    Set RSSItem = RSSItems.Item(i) 
    FOR EACH objChild IN RSSItem.childNodes 
     IF nCell = 3 THEN 
     paryOut(0,i) = objChild.text 
     ELSEIF nCell = 5 THEN 
     paryOut(1,i) = objChild.text 
     ELSEIF nCell = 7 THEN 
     paryOut(2,i) = objChild.text 
     ELSEIF nCell = 27 THEN 
     paryOut(3,i) = objChild.text 
     ELSEIF nCell = 23 THEN 
     paryOut(4,i) = objChild.text 
     ELSEIF nCell = 40 THEN 
     paryOut(5,i) = objChild.text 
     END IF 
     nCell = nCell + 1 
    NEXT 
    nCell = 0 
    NEXT 
    FetchProductRecords = paryOut 
END FUNCTION 

paryProducts = FetchProductRecords(nFileName) 
IF isArray(paryProducts) THEN 
%> 
<table cellpadding="0" cellspacing="0" border="1" width="100%"> 
    <tr style="background-color:#000000;color:#FFFFFF;font-weight:bold;font-family:Arial, Helvetica, sans-serif;font-size:10px"> 
    <td style="padding:5px" align="center">DOBA ID</td> 
    <td style="padding:5px" align="center">TITLE</td> 
    <td style="padding:5px" align="center">DESCRIPTION</td> 
    <td style="padding:5px" align="center">PRICE</td> 
    <td style="padding:5px" align="center">WEIGHT</td> 
    <td style="padding:5px" align="center">IMAGE</td> 
    </tr> 
    <% FOR i = 0 TO uBound(paryProducts,2) %> 
    <% IF i > 0 THEN ' to avoid the header %> 
    <tr style="font-family:Arial, Helvetica, sans-serif;font-size:10px"> 
    <td style="padding:5px"><%=paryProducts(0,i)%></td> 
    <td style="padding:5px"><%=paryProducts(1,i)%></td> 
    <td style="padding:5px"><%=paryProducts(2,i)%></td> 
    <td style="padding:5px"><%=paryProducts(3,i)%></td> 
    <td style="padding:5px"><%=paryProducts(4,i)%></td> 
    <td style="padding:5px"><%=paryProducts(5,i)%></td> 
    </tr> 
    <% END IF %> 
    <% NEXT %> 
</table> 
<% END IF %> 

總而言之,所有的XML文件都有40個字段。你可以看看這裏的xml文件:http://www.storeboard.com/excel/cameras2.xml

我將不勝感激任何幫助,這個代碼,所以我終於可以完成它。

有一個偉大的一天,聖誕快樂, 保羅

回答

0

你必須設置nFileName爲「cameras2.xml」,對不對?

nFileName = "cameras2.xml" 
paryProducts = FetchProductRecords(nFileName) 

除此之外,它的工作原理,從我的機器偉大的..

如果您的應用程序和XML駐留在同一個網站,然後查看這篇文章INFO: Do Not Send ServerXMLHTTP or WinHTTP Requests to the Same Server

+0

它也適用於我的機器,但我不認爲這是@ neojakey的問題,因爲在進一步檢查數據時,我可以看到它與單元格編號不匹配。這當然假設他的應用程序不會在檢索1.17 MB XML文件時超時。 ;-) – stealthyninja 2010-12-22 21:07:22

0

@neojakey:我看到的問題是每行的單元格數目不匹配。在第一行('header')中,有45個單元格。後續的每一行都有較少的單元格,但從未達到45,並且它們看起來也不相關。我已經排到了第二行數據與第一行向你展示我的意思:

+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 0 | supplier_id   | 4                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 1 | drop_ship_fee   | 0                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 2 | supplier_name   | Diamond                        | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 3 | product_id    | 17697                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 4 | product_sku   | BCR10                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 5 | title     | OmniSource 10 12 AC/DC Charger                  | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 6 | warranty    | OmniSource 10 12 AC/DC Charger/Reconditioner for VHS &amp; VHS-C (9.6V to 12V) full size camcorders | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 7 | description   | new                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 8 | condition    | Lenmar                        | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 9 | details    | 0                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 10 | manufacturer   | 2010-12-19T17:24:23.000                    | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 11 | brand_name    | 22091                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 12 | case_pack_quantity  | BCR10                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 13 | country_of_origin  | 2147483647                       | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 14 | product_last_update | 2147483647                       | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 15 | item_id    | OmniSource 10 12 AC/DC Charger                  | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 16 | item_sku    | 0                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 17 | mpn     | 0                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 18 | upc     | 1.95                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 19 | item_name    | 8.27                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 20 | item_weight   | 28.01                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 21 | ship_alone    | 27.2                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 22 | ship_freight   | 69.95                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 23 | ship_weight   | 4                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 24 | ship_cost    | in-stock                        | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 25 | max_ship_single_box | 0                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 26 | map     | 2010-12-16T15:06:25.000                    | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 27 | price     | Catalog||Electronics &amp; computer||Camera &amp; photo||Digital camera accessories     | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 28 | custom_price   | http://images.doba.com/products/4/images_prodLarge_BCR10.jpg           | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 29 | prepay_price   | 400                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 30 | street_price   | 400                         | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 31 | msrp     | 0                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 32 | qty_avail    |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 33 | stock     |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 34 | est_avail    |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 35 | qty_on_order   |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 36 | item_last_update  |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 37 | item_discontinued_date |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 38 | categories    |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 39 | attributes    |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 40 | image_file    |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 41 | image_width   |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 42 | image_height   |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 43 | additional_images  |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 
| 44 | is_customized   |                          | 
+----+------------------------+-------------------------------------------------------------------------------------------------------+ 

由於product_id從來沒有從#3移動和title從不#5移動時,你的應用程序似乎是工作。但是,由於數據不匹配,descriptionprice,weightimage字段被破壞。如果更新,你遍歷RSSItem.childNodes

IF nCell = 3 THEN 
    paryOut(0,i) = objChild.text 'product_id' 
ELSEIF nCell = 5 THEN 
    paryOut(1,i) = objChild.text 'title' 
ELSEIF nCell = 6 THEN 
    paryOut(2,i) = objChild.text 'description' 
ELSEIF nCell = 27 THEN 
    paryOut(3,i) = objChild.text 'price - could be ?' 
ELSEIF nCell = 23 THEN 
    paryOut(4,i) = objChild.text 'weight - could be ?' 
ELSEIF nCell = 28 THEN 
    paryOut(5,i) = objChild.text 'image' 
END IF 

...這個問題將許多您的產品代碼,你至少得到正確的description,而不是產品的condition,以及image_file往往比從來沒有得到它。

然而,問題的解決方案是首先獲取生成數據的東西,以便生成<Cell>0</Cell>,前提是它無法找到它應該顯示的標題單元格的值,而不僅僅是不輸出它在所有這些都與你的應用程序的硬編碼邏輯混淆了。