使用经典ASP显示XML数据

时间:2010-12-22 17:20:42

标签: xml asp-classic rss

早上好,

我正在努力让这段代码发挥作用。我花了几个小时调整,改变它......它仍然无法工作。

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

我很感激您对此代码的任何帮助,以便我最终能够完成它。

祝你度过美好的一天,圣诞快乐, 保罗

2 个答案:

答案 0 :(得分:0)

您确实已将nFileName设为“ cameras2.xml ”,对吗?

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

除此之外,它在我的机器上运行良好..

如果您的应用和XML位于同一网站上,请查看此文章INFO: Do Not Send ServerXMLHTTP or WinHTTP Requests to the Same Server

答案 1 :(得分:0)

@neojakey:我看到的问题是每行中的单元格数不匹配。在第一行(“标题”)中,有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移动,因此您的应用程序似乎正在运行。但是,descriptionpriceweightimage字段由于数据不匹配而中断。如果您更新循环通过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>,那么首先要生成数据。完全输出它会弄乱应用程序的硬编码逻辑。