Parse XML from ASP!!!

Reply

Join Date: Aug 2005
Posts: 12
Reputation: Mr Cuzac is an unknown quantity at this point 
Solved Threads: 0
Mr Cuzac's Avatar
Mr Cuzac Mr Cuzac is offline Offline
Newbie Poster

Parse XML from ASP!!!

 
0
  #1
Aug 30th, 2005
What is the best script and/or class to parse an XML file from an ASP page?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 89
Reputation: msaqib is an unknown quantity at this point 
Solved Threads: 1
msaqib msaqib is offline Offline
Junior Poster in Training

Re: Parse XML from ASP!!!

 
0
  #2
Nov 7th, 2005
Here is a class that parse the RSS XML.
  1. <%
  2. '+---------------------------------------------+
  3. '| RSS Content Feed VBScript Class 1.0 |
  4. '| © 2004 <a rel="nofollow" class="t" href="http://www.tele-pro.co.uk" target="_blank">www.tele-pro.co.uk</a> |
  5. '| <a rel="nofollow" class="t" href="http://www.tele-pro.co.uk/scripts/rss/" target="_blank">http://www.tele-pro.co.uk/scripts/rss/</a> |
  6. '| The RSSContentFeed Class makes it easy to |
  7. '| download and display RSS XML feeds. |
  8. '+---------------------------------------------+
  9.  
  10. Class RSSContentFeed
  11. '+---------------------------------------------+
  12. 'declare class variables
  13. 'strings
  14. Private classname
  15. Private xml_URL
  16. Private xml_data
  17. Private StrResultsXML
  18. Private StrCachePath
  19. Private Strchannel
  20. Private Strtitle
  21. Private Strlink
  22. Private Strdescription
  23. Private StrRSSVersion
  24. Private imgTitle
  25. Private imgUrl
  26. Private imgLink
  27. 'ebay
  28. Private eBayAPIURL
  29. Private eBayAPISandboxURL
  30. Private imgBuyItNow
  31.  
  32. Public eBayTime 'date
  33. 'int
  34. Private iTotalResults
  35. Private icacheDays
  36. Private iMaxResults
  37. Private imgWidth
  38. Private imgHeight
  39.  
  40. 'bool
  41. Private bFromcache
  42.  
  43. 'dict
  44. Private Headers
  45. 'arrays
  46. Public Results()
  47. Public Links()
  48. Public Titles()
  49. Public Descriptions()
  50. Public PubDates()
  51. Public Images()
  52. Public Ids()
  53.  
  54. '+---------------------------------------------+
  55. 'Class Functions
  56. 'Class_Initialize
  57. Private Sub Class_Initialize
  58. Initialize
  59. End Sub
  60.  
  61. 'Class_Terminate
  62. Private Sub Class_Terminate
  63. 'empty the cache
  64. DeleteCache()
  65. 'empty the dict
  66. If IsObject(Headers) Then
  67. Headers.RemoveAll
  68. Set Headers = Nothing
  69. End If
  70.  
  71. End Sub
  72. Public Sub Initialize
  73. 'set constant values
  74. classname = "RSSContentFeed"
  75. eBayAPIURL = "<a rel="nofollow" class="t" href="https://api.ebay.com/ws/api.dll" target="_blank">https://api.ebay.com/ws/api.dll</a>"
  76. eBayAPISandboxURL = "<a rel="nofollow" class="t" href="https://api.sandbox.ebay.com/ws/api.dll" target="_blank">https://api.sandbox.ebay.com/ws/api.dll</a>"
  77. imgBuyItNow = "<a rel="nofollow" class="t" href="http://pics.ebaystatic.com/aw/pics/promo/holiday/buyItNow_15x54.gif" target="_blank">http://pics.ebaystatic.com/aw/pics/p...tNow_15x54.gif</a>"
  78.  
  79. 'set object vars
  80. xml_URL = ""
  81. xml_data = ""
  82. StrCachePath = ""
  83. icacheDays = 1
  84. iMaxResults = 15
  85.  
  86. 'clear result vars
  87. Set Headers = Createobject("Scripting.Dictionary")
  88. Clear()
  89. End Sub
  90. '+---------------------------------------------+
  91. Public Sub Clear
  92. 'Clear search variables
  93. iTotalResults =0
  94. bFromcache = false
  95. Strlink = ""
  96. Strtitle = ""
  97. Strdescription = ""
  98. 'channel image
  99. imgTitle = ""
  100. imgUrl = ""
  101. imgLink = ""
  102. imgWidth = 0
  103. imgHeight = 0
  104.  
  105. eBayTime = ""
  106.  
  107. ReDim Results(1)
  108. ReDim Links(1)
  109. ReDim Titles(1)
  110. ReDim Descriptions(1)
  111. ReDim PubDates(1)
  112. ReDim Images(1)
  113. ReDim Ids(1)
  114. End Sub
  115. '+---------------------------------------------+
  116. 'Public Properties - Readonly
  117. 'show the copyright info
  118. Public Property Get Version
  119. Version = "XML RSS Content Feed VBScript Class Version 1.0 " & VbCrLf & _
  120. "© 2004 <a rel="nofollow" class="t" href="http://www.tele-pro.co.uk" target="_blank">www.tele-pro.co.uk</a>"
  121. End Property
  122. Public Property Get TotalResults
  123. TotalResults = iTotalResults
  124. End Property
  125. Public Property Get CacheCount
  126. CacheCount = CacheContentCount(StrCachePath)
  127. End Property
  128. Public Property Get Fromcache
  129. Fromcache= (bFromcache = true)
  130. End Property
  131. Public Property Get ChannelLink
  132. ChannelLink= Trim(Strlink)
  133. End Property
  134. Public Property Get ChannelTitle
  135. ChannelTitle= Trim(Strtitle)
  136. End Property
  137. Public Property Get ChannelDescription
  138. ChannelDescription = Trim(Strdescription)
  139. End Property
  140. Public Property Get ChannelImgURL
  141. ChannelImgURL = Trim(imgURL)
  142. End Property
  143. Public Property Get ChannelImgTitle
  144. ChannelImgTitle = Trim(imgTitle)
  145. End Property
  146. Public Property Get ChannelImgLink
  147. ChannelImgLink = Trim(imgLink)
  148. End Property
  149. Public Property Get ChannelImgWidth
  150. ChannelImgWidth = CLNG(imgWidth)
  151. End Property
  152. Public Property Get ChannelImgHeight
  153. ChannelImgHeight = CLNG(imgHeight)
  154. End Property
  155. Public Property Get ResultsXML
  156. ResultsXML = Trim(strResultsXML)
  157. End Property
  158. Public Property Get RSSVersion
  159. RSSVersion = Trim(strRSSVersion)
  160. End Property
  161. '+---------------------------------------------+
  162. 'Public Properties - settable
  163. 'show the xml_URL
  164. Public Property Get ContentURL
  165. ContentURL = Trim(xml_URL)
  166. End Property
  167. 'set the xml_URL
  168. Public Property Let ContentURL(ByVal vContentURL)
  169. vContentURL = Trim(vContentURL)
  170. 'add protocol if necessary
  171. If inStr(LCASE(vContentURL), "<A href="http://")=0">http://")=0 Then
  172. vContentURL = "http://" & vContentURL
  173. End if
  174. xml_URL = Trim(vContentURL)
  175. End Property
  176. Public Property Get PostData
  177. PostData = Trim(xml_data)
  178. End Property
  179. Public Property Let PostData(sxml_data)
  180. xml_data = Trim(sxml_data)
  181. End Property
  182. Public Property Get Cache
  183. Cache = Trim(StrCachePath)
  184. End Property
  185. Public Property Let Cache(ByVal sCache)
  186. StrCachePath = ""
  187. If Trim(sCache)<>"" Then
  188.  
  189. If Not DExists(sCache) Then
  190. ErrRaise "SetCache" , "Cache folder does not exist "
  191. Else
  192. 'rem last slash
  193. If (Mid(sCache, LEN(sCache), 1) = "\") Then
  194. sCache = Mid(sCache, 1, LEN(sCache)-1)
  195. End If
  196. 'add slash
  197. StrCachePath = Trim(sCache) & "\"
  198. End If
  199. End If
  200. End Property
  201. Public Property Get CacheDays
  202. CacheDays = CLNG(iCacheDays)
  203. End Property
  204. Public Property Let CacheDays(iDays)
  205. iCacheDays = CLNG(iDays)
  206. End Property
  207. Public Property Get MaxResults
  208. MaxResults = CLNG(iMaxResults)
  209. End Property
  210. Public Property Let MaxResults(vMaxResults)
  211. iMaxResults = CLNG(vMaxResults)
  212. End Property
  213. '+---------------------------------------------+
  214. 'Public Functions
  215. 'Delete items in Cache
  216. Public FUNCTION DeleteCache()
  217. If (Trim(StrCachePath)<>"") Then
  218. DeleteCache = DeleteCacheContent(StrCachePath, icacheDays)
  219. End If
  220. End FUNCTION
  221. 'add header for http request
  222. Public FUNCTION AddHeader(str_hdr, str_val)
  223. 'add header to dict for http request
  224. If Not (Headers.Exists(Trim(str_hdr))) Then
  225. Headers.Add Trim(str_hdr), Trim(str_val)
  226. Else
  227. Headers(str_hdr) = Trim(str_val)
  228. End If
  229. End FUNCTION
  230. 'transform xml with xsl
  231. Public FUNCTION Transform(str_xslt)
  232. If Trim(StrResultsXML)="" Then Exit Function
  233. If Trim(str_xslt)="" Then Exit Function
  234.  
  235. 'Load XML
  236. Dim x
  237. set x = CreateObject("MSXML2.DOMDocument")
  238. x.async = false
  239. x.setProperty "ServerHTTPRequest", True
  240. 'path or url?
  241. If (inStr(str_xslt, "http")=1) Then 'url
  242. Dim tmpStr
  243. tmpStr = getResults(str_xslt)
  244. x.LoadXML(tmpStr)
  245. Else
  246. If (inStr(str_xslt, "\")=0) Then 'needs mapping
  247. str_xslt = Server.MapPath(str_xslt)
  248. x.Load(str_xslt)
  249. End if
  250. End if
  251. x.resolveExternals = False
  252.  
  253. If (x.parseError.errorCode <> 0) Then
  254. ErrRaise "Transform", "XML error: " & x.parseError.reason
  255. EXIT FUNCTION
  256. End If
  257. str_xslt = x.xml
  258. Transform = TransformXML(StrResultsXML, str_xslt)
  259. End FUNCTION
  260. 'retrieve the value of a node
  261. Public FUNCTION XMLValue(str_node)
  262. If Trim(StrResultsXML)="" Then Exit Function
  263. XMLValue = GetNodeText(str_node, StrResultsXML)
  264. End FUNCTION
  265. 'construct amazon rss url and call getrss function
  266. Public Function GetAmazonRSS(t, devt, kwd, mode, bcm)
  267. 'check
  268. If Trim(t) = "" Then
  269. ErrRaise "GetAmazonRSS", "Associate tag must be set"
  270. Exit Function
  271. End if
  272. If Trim(devt) = "" Then
  273. ErrRaise "GetAmazonRSS", "Developer token must be set"
  274. Exit Function
  275. End if
  276. If Trim(kwd) = "" Then
  277. ErrRaise "GetAmazonRSS", "KeywordSearch token must be set"
  278. Exit Function
  279. End if
  280. If Trim(mode) = "" Then
  281. mode = "books"
  282. End if
  283.  
  284. 'set amazon vals
  285. xml_url = "<a rel="nofollow" class="t" href="http://xml-na.amznxslt.com/onca/xml3" target="_blank">http://xml-na.amznxslt.com/onca/xml3</a>" & _
  286. "?t=" & Trim(t) & _
  287. "&dev-t=" &Trim(devt) & _
  288. "&KeywordSearch=" & Trim(kwd) & _
  289. "&mode=" & Trim(mode) & _
  290. "&bcm=" & Trim(bcm) & _
  291. "&type=lite" & _
  292. "&page=1" & _
  293. "&ct=text/xml" & _
  294. "&sort=%2Bsalesrank" & _
  295. "&f=http://www.tele-pro.co.uk/scripts/rss/amazon.xsl"
  296. '"&f=http://xml.amazon.com/xsl/xml-rss091.xsl"
  297.  
  298. GetAmazonRSS = GetRSS()
  299. End Function
  300. '+---------------------------------------------+
  301. 'main function
  302. Public Function GetRSS()
  303. 'clear search
  304. Clear()
  305.  
  306. 'check xml_URL
  307. If Trim(xml_URL) = "" Then
  308. ErrRaise "GetRSS", "ContentURL must be set"
  309. End if
  310.  
  311. 'get results from web or cache
  312. Dim soapResults, soapResultsStd
  313. soapResults = getResults(xml_URL)
  314.  
  315. 'Dump the results into an XML document.
  316. Dim Res
  317. Set Res = CreateObject("MSXML2.DOMDocument")
  318. Res.async = false
  319.  
  320. 'set the global xml string
  321. StrResultsXML = Trim(soapResults)
  322. soapResultsStd = DeSensitize(soapResults)
  323.  
  324. Res.setProperty "ServerHTTPRequest", True
  325. Res.loadXML soapResultsStd
  326. Res.resolveExternals = False
  327.  
  328. If (Res.parseError.errorCode <> 0) Then
  329. ErrRaise "GetRSS", "XML error: " & Res.parseError.reason
  330. EXIT FUNCTION
  331. End If
  332.  
  333. 'set the global xml string to the xml formatted string
  334. If Trim(soapResultsStd) = Trim(soapResults) Then
  335. StrResultsXML = Trim(Res.XML)
  336. End If
  337.  
  338. Dim Node, Nodes
  339. '---------------------------------------------------------
  340. 'get RSS Version
  341.  
  342. StrRSSVersion = ""
  343. Set Nodes = Res.selectNodes("//rss")
  344. For Each Node In Nodes
  345. on error resume next
  346. strRSSVersion = Node.getAttribute("version")
  347. on error Goto 0
  348. Next
  349.  
  350. if (Trim(strRSSVersion)="") Then
  351. Set Nodes = Res.selectNodes("//eBay")
  352. For Each Node In Nodes
  353. strRSSVersion = "eBay"
  354. Next
  355. end if
  356.  
  357. if (Trim(strRSSVersion)="") Then
  358. Set Nodes = Res.selectNodes("//rdf:RDF")
  359. For Each Node In Nodes
  360. on error resume next
  361. strRSSVersion = Node.getAttribute("xmlns")
  362. If Trim(strRSSVersion) = "<a rel="nofollow" class="t" href="http://purl.org/rss/1.0/" target="_blank">http://purl.org/rss/1.0/</a>" Then
  363. strRSSVersion = "1.0"
  364. End If
  365. on error Goto 0
  366. Next
  367. end if
  368.  
  369. if (Trim(strRSSVersion)="eBay") Then
  370. Set Nodes = Res.selectNodes("//eBayTime")
  371. For Each Node In Nodes
  372. eBayTime = Node.Text
  373. Next
  374. end if
  375.  
  376. '---------------------------------------------------------
  377.  
  378. 'set the size of arrays to the max results
  379. Dim c
  380. c=0
  381.  
  382. 'get the size
  383. Set Nodes = Res.selectNodes("//item")
  384. For Each Node In Nodes
  385. If (c<iMaxResults) Then
  386. c = c + 1
  387. End If
  388. Next
  389.  
  390. 'set the size
  391. ReDim Results(c-1)
  392. ReDim Links(c-1)
  393. ReDim Titles(c-1)
  394. ReDim Descriptions(c-1)
  395. ReDim PubDates(c-1)
  396. ReDim Images(c-1)
  397. ReDim Ids(c-1)
  398.  
  399. 'get item content
  400. 'declare results strings
  401. Dim res_URL
  402. Dim res_title
  403. Dim res_desc
  404. Dim res_date
  405. Dim res_img
  406. Dim res_id
  407. 'ebay
  408. Dim CurrencyId, CurrentPrice, BidCount
  409.  
  410. 'Parse the XML document.
  411. c=0
  412. For Each Node In Nodes
  413. If (c<iMaxResults) Then
  414.  
  415. 'clear the strings
  416. res_URL = ""
  417. res_title = ""
  418. res_desc = ""
  419. res_date = ""
  420. res_img = ""
  421. res_id = ""
  422. CurrencyId = ""
  423. CurrentPrice = ""
  424. BidCount = ""
  425.  
  426. 'retrieve the values
  427. on error resume next
  428. res_URL = Trim(Node.selectSingleNode("link").Text)
  429. res_title = Trim(Node.selectSingleNode("title").Text)
  430. res_desc = Trim(Node.selectSingleNode("description").XML)
  431. 'amazon from custom xsl
  432. res_img = Trim(Node.selectSingleNode("imgS").Text)
  433. res_id = Trim(Node.selectSingleNode("Asin").Text)
  434. on error goto 0
  435.  
  436. 'or it might be a dc:description tag
  437. If (Trim(res_desc)="") Then
  438. on error resume next
  439. res_desc = Trim(Node.selectSingleNode("dc:description").XML)
  440. on error goto 0
  441. End If
  442.  
  443. res_desc = Replace(res_desc, "<description>", "")
  444. res_desc = Replace(res_desc, "</description>", "")
  445.  
  446. 'or it might be ebay
  447. If (strRSSVersion = "eBay") Then
  448. If (Trim(res_desc)="") Then
  449.  
  450. 'get ebay data
  451. on error resume next
  452. CurrencyId = Trim(Node.selectSingleNode("CurrencyId").Text)
  453. CurrentPrice = Trim(Node.selectSingleNode("CurrentPrice").Text)
  454. BidCount = Trim( Node.selectSingleNode("BidCount").Text)
  455. res_img = Trim(Node.selectSingleNode("ItemProperties//GalleryURL").Text)
  456. res_id = Trim( Node.selectSingleNode("Id").Text)
  457. on error goto 0
  458.  
  459. res_desc = res_desc & "<b>"
  460. res_desc = res_desc & eBayCurrencySymbolFromID(CurrencyId)
  461. res_desc = res_desc & Trim(CurrentPrice) & "</b> ("
  462. res_desc = res_desc & Trim(BidCount) & " bids) " & VbCrLf
  463.  
  464. 'construct description
  465. on error resume next
  466. If Trim(Node.selectSingleNode("ItemProperties//BuyItNow").Text)="1" Then
  467. res_desc = res_desc & " &nbsp;<a href="""
  468. res_desc = res_desc & res_URL
  469. res_desc = res_desc & """><img align=""absmiddle"" border=""0"" src="""
  470. res_desc = res_desc & imgBuyItNow
  471. res_desc = res_desc & """ alt=""Buy It Now""></a>" & VbCrLf
  472. End If
  473. on error goto 0
  474.  
  475. 'ItemProperties//Featured
  476. 'ItemProperties//New
  477. 'ItemProperties//IsFixedPrice
  478. 'ItemProperties//Gift
  479. 'ItemProperties//CharityItem
  480.  
  481. End If
  482. End If '(strRSSVersion = "eBay")
  483. 'optional tags
  484. on error resume next
  485. res_date = Node.selectSingleNode("pubDate").Text
  486. 'ebay
  487. If (Trim(res_date)="") Then
  488. res_date = Node.selectSingleNode("EndTime").Text
  489. End If
  490. on error goto 0
  491.  
  492. if Trim(res_URL)<>"" Or _
  493. Trim(res_title)<>"" Or _
  494. Trim(res_desc)<>"" then
  495.  
  496. 'its a result, add to array
  497. Results(c) = c
  498. Links(c) = res_URL
  499. Titles(c) = res_title
  500. Descriptions(c) = res_desc
  501. PubDates(c) = res_date
  502. Images(c) = res_img
  503. Ids(c) = res_id
  504.  
  505. c=c+1 'inc counter
  506. End If
  507. End If
  508. Next
  509.  
  510. '---------------------------------------------------------
  511.  
  512. 'get channel content
  513. Set Nodes = Res.selectNodes("//channel")
  514. For Each Node In Nodes
  515. on error resume next
  516. Strlink = Node.selectSingleNode("link").Text
  517. Strtitle = Node.selectSingleNode("title").Text
  518. Strdescription = Node.selectSingleNode("description").Text
  519. on error Goto 0
  520. Next
  521.  
  522. 'get image
  523. Set Nodes = Res.selectNodes("//image")
  524. For Each Node In Nodes
  525. on error resume next
  526. imgTitle = Node.selectSingleNode("title").Text
  527. imgUrl = Node.selectSingleNode("url").Text
  528. imgLink = Node.selectSingleNode("link").Text
  529. imgWidth = Node.selectSingleNode("width").Text
  530. imgHeight = Node.selectSingleNode("height").Text
  531. on error Goto 0
  532. Next
  533.  
  534. 'release objects
  535. Set Nodes = Nothing
  536. Set Res = Nothing
  537.  
  538. 'return count
  539. iTotalResults = c
  540. GetRSS = c
  541. End Function
  542. Private Function DeSensitize(Istr)
  543. Dim str
  544. str = Istr
  545. str = Replace(str, "<Item>", "<item>", 1, -1, 1)
  546. str = Replace(str, "<Link>", "<link>", 1, -1, 1)
  547. str = Replace(str, "<Title>", "<title>", 1, -1, 1)
  548. str = Replace(str, "</Item>", "</item>", 1, -1, 1)
  549. str = Replace(str, "</Link>", "</link>", 1, -1, 1)
  550. str = Replace(str, "</Title>", "</title>", 1, -1, 1)
  551. DeSensitize = str
  552. End Function
  553. Public Function ItemHTML(iNumber)
  554. Dim r_URL, r_title, r_description, r_pubdate
  555.  
  556. If (iTotalResults=0) Then
  557. ErrRaise "ItemHTML", "There are no items"
  558. Exit Function
  559. End If
  560. If (iNumber>=iTotalResults) Then
  561. ErrRaise "ItemHTML", "Item index out of bounds"
  562. Exit Function
  563. End If
  564.  
  565. r_URL = Links(iNumber)
  566. r_title= Titles(iNumber)
  567. r_description = Descriptions(iNumber)
  568. r_pubdate = PubDates(iNumber)
  569.  
  570. ItemHTML = Trim(FormatResult(r_URL, r_title, r_description, r_pubdate))
  571. End Function
  572.  
  573. Private Function FormatResult(h, t, d, p)
  574. Dim str
  575. str = ""
  576. str = str & "<b><a href=""" & h & """>" & t & "</a></b> <br/> " & VbCrLF
  577. If (Trim(d) <> "") Then str = str & Shorten(d, 25, "...") & "<br/>" & VbCrLF
  578. str = str & "<a href=""" & h & """>" & h & "</a>" & VbCrLF
  579. If (Trim(p) <> "") Then str = str & "<br/>" & p & VbCrLF
  580. FormatResult= Trim(str)
  581. End Function
  582. '+---------------------------------------------+
  583. 'Private Functions
  584. Private Function ErrRaise(f, e)
  585. Err.Raise vbObjectError+1001, classname, f & ": " & e
  586. Response.End
  587. End Function
  588. Private Function GetXMLResults(q)
  589. GetXMLResults = XmlHttp( (q), xml_data, Headers)
  590. 'Server.URLEncode
  591. End Function
  592. 'get results from cache or from web
  593. Private FUNCTION qCheckSum(d)
  594. 'quick checksum
  595. Dim chks
  596. chks = 0
  597. Dim x
  598. For x = 1 To LEN(d)
  599. chks = chks + ( (ASC(Mid(d, x, 1))) * (x Mod 255) )
  600. Next
  601. qCheckSum = CLNG(chks)
  602. End Function
  603. 'get results from cache or from web
  604. Private FUNCTION getResults(q)
  605. Dim res, a
  606. a = CacheFileName(q & xml_data)
  607. res = ""
  608.  
  609. If (Trim(StrCachePath)<>"") Then res = ReadFile(a)
  610. If (Trim(res) = "") Then
  611. res = getXMLResults(q)
  612.  
  613. 'after many problems passing string straight back
  614. 'writing and reading back solved the problem
  615. Dim b
  616. b = Server.MapPath("_rss_content_feed_class_1_tmp.txt")
  617. Call DelFile(b)
  618. Call Write2File(b, res)
  619. res = ReadFile(b)
  620. Call DelFile(b)
  621.  
  622. If (Trim(StrCachePath)<>"") Then Call Write2File(a, res)
  623. bFromcache = False
  624. Else
  625. bFromcache = True
  626. End if
  627.  
  628. getResults = res
  629. END FUNCTION
  630. Private FUNCTION CacheFileName(n)
  631. Dim cn
  632. Dim cd
  633. cn = qCheckSum(n)
  634. cd = DomainFromUrl(n)
  635. cn = StrCachePath & cd & "~" & cn & ".xml"
  636. CacheFileName = cn
  637. End FUNCTION
  638. Private Function DomainFromUrl(sText)
  639. Dim nIndex
  640. If (LCase(Left(sText, 7))) = "http://" Then sText = Mid(sText, 8)
  641. If LCase(Left(sText, 8 )) = "https://" Then sText = Mid(sText, 9)
  642. nIndex = InStr(sText, "/")
  643. If (nIndex > 0) Then sText = Left(sText, nIndex - 1)
  644. DomainFromUrl = sText
  645. End Function
  646. Private FUNCTION CacheContentCount(cache)
  647. CacheContentCount = 0
  648. If Trim(cache)="" Then Exit FUNCTION
  649. If Not DExists(cache) Then Exit FUNCTION
  650. CacheContentCount = CLNG(FolderCount(cache))
  651. End FUNCTION
  652. Private FUNCTION DeleteCacheContent(cache, age)
  653. If Trim(cache)="" Then Exit FUNCTION
  654. If Not DExists(cache) Then Exit FUNCTION
  655.  
  656. 'count cache
  657. Dim a
  658. a = CacheContentCount(cache)
  659.  
  660. Dim fs
  661. Set fs = Createobject("Scripting.FileSystemobject")
  662. Dim oFolder
  663. Set oFolder = fs.GetFolder(cache)
  664. Dim oFile
  665. For Each oFile in oFolder.Files
  666. If (age <= (Int(Now() - oFile.DateLastModified))) Then
  667. oFile.Delete True
  668. End If
  669. Next
  670. Set fs = Nothing
  671. Set oFolder = Nothing
  672. 'count cache
  673. a = (CLNG(a) - CLNG(CacheContentCount(cache)))
  674.  
  675. DeleteCacheContent = CLNG(a)
  676. END FUNCTION
  677. '+---------------------------------------------+
  678. 'Generic
  679. 'Retrieve response and return HTML response body
  680. Public Function XmlHttp(xAction, data, hdrs)
  681. Dim HTTP, Raw
  682. Set Http = CreateObject("MSXML2.ServerXMLHTTP")
  683. 'MSXML2.XMLHTTP
  684.  
  685. if (Trim(data) <> "") then
  686. Http.open "POST", xAction, FALSE
  687.  
  688. 'add post hdr
  689. if (inStr(data, "<?xml")=1) then
  690. Http.setRequestHeader "Content-Type","text/xml"
  691. else
  692. Http.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
  693. end if
  694. Http.setRequestHeader "Content-Length",Len(data)
  695. else
  696.  
  697. Http.open "GET", xAction, FALSE
  698. end if
  699. 'get headers from the dict
  700. If IsObject(hdrs) Then
  701. Dim hdr
  702. For Each hdr in hdrs
  703. Http.setRequestHeader Trim(hdr), Trim(hdrs(hdr))
  704. Next
  705. End If
  706. Http.send (data)
  707. Raw = http.responseText
  708. Set Http = Nothing
  709. XmlHttp = Raw
  710. End Function
  711. Private Function DExists(d) 'true if file exists
  712. Dim fso
  713. Set fso = CreateObject("Scripting.FileSystemObject")
  714. DExists = fso.FolderExists(d)
  715. Set fso = Nothing
  716. End Function
  717.  
  718. Private Function FExists(d) 'true if file exists
  719. Dim fso
  720. Set fso = CreateObject("Scripting.FileSystemObject")
  721. FExists = fso.FileExists(d)
  722. Set fso = Nothing
  723. End Function
  724.  
  725. Private Function DelFile(f)
  726. If Trim(f)="" Then Exit FUNCTION
  727. Dim fso
  728. Set fso = CreateObject("Scripting.FileSystemObject")
  729. if FExists(f) then fso.DeleteFile(f)
  730. Set fso = Nothing
  731. End Function
  732. Private FUNCTION FolderCount(dir)
  733. If Trim(dir)="" Then Exit FUNCTION
  734. Dim fs
  735. Set fs = Createobject("Scripting.FileSystemobject")
  736. Dim oFolder
  737. Set oFolder = fs.GetFolder(dir)
  738. FolderCount = oFolder.Files.Count
  739. Set fs = Nothing
  740. Set oFolder = Nothing
  741. END FUNCTION
  742. Private Function Write2File(afile,bstr)
  743. Dim wObj, wText
  744. if afile="" Then EXIT FUNCTION
  745. Set wObj = CreateObject("Scripting.FileSystemObject")
  746. Set wtext = wObj.OpenTextFile(afile, 8, True)
  747. Dim nCharPos, sChar
  748. For nCharPos = 1 To Len(bstr)
  749. sChar = Mid(bstr, nCharPos, 1)
  750. On Error resume next '<-- **** Error handing starts ****
  751. wtext.Write sChar
  752. On Error Goto 0 '<-- ***** Error handing ends *****
  753. Next
  754. wtext.Close()
  755. Set wtext = Nothing
  756. Set wObj = Nothing
  757. End Function
  758. Private Function ReadFile(fpath)
  759. Dim fObj, ftext, fileStr
  760. Set fObj = CreateObject("Scripting.FileSystemObject")
  761. If fObj.FileExists(fpath) Then
  762. Set ftext = fObj.OpenTextFile(fpath, 1, FALSE)
  763. fileStr =""
  764. WHILE NOT ftext.AtEndOfStream
  765. fileStr = fileStr & ftext.ReadLine & chr(13)
  766. WEND
  767. ftext.Close
  768. else
  769. fileStr = ""
  770. End if
  771. ReadFile= fileStr
  772. End Function
  773. Public Function Shorten(sentence, wds, addifShortened)
  774. Dim ret
  775. ret = Trim(sentence)
  776. Dim ar
  777. ReDim ar(1)
  778. ar = Split(ret)
  779. ret = ""
  780. Dim c
  781. For c = 0 To UBOUND(ar)
  782. If c < wds Then
  783. ret = ret & " " & ar(c)
  784. End If
  785. Next
  786. ret = Trim(ret)
  787. If Trim(ret) <> Trim(sentence) Then
  788. ret = ret & addifShortened
  789. End If
  790. Shorten = ret
  791. End Function
  792.  
  793. Private FUNCTION GetNodeText(str_node, str_xml)
  794. Dim tmpString
  795. tmpString = Trim(str_xml)
  796. 'declare an xml object to work with
  797. dim xmldoc
  798. set xmldoc = CreateObject("MSXML2.DOMDocument")
  799. xmldoc.async = False
  800. xmldoc.setProperty "ServerHTTPRequest", True
  801.  
  802. 'attempt to load from str
  803. xmldoc.LoadXML(tmpString)
  804. xmldoc.resolveExternals = False
  805.  
  806. If (xmldoc is Nothing) Or (Len(xmldoc.text) = 0) then
  807. 'error
  808. EXIT FUNCTION
  809. End If
  810. 'attempt to get Node Text
  811. Dim currNode
  812. tmpString = ""
  813. Set currNode = xmlDoc.documentElement.selectSingleNode(str_node)
  814. On Error Resume next
  815. tmpString = Trim(currNode.Text)
  816. On Error Goto 0
  817. Set currNode = Nothing
  818.  
  819. GetNodeText = Trim(tmpString)
  820. END FUNCTION
  821. 'Transform XML with XSL string
  822. Private FUNCTION TransformXML(xml, xslt)
  823. 'Load XML
  824. Dim x
  825. set x = CreateObject("MSXML2.DOMDocument")
  826. x.async = false
  827. x.setProperty "ServerHTTPRequest", True
  828.  
  829. x.LoadXML(xml)
  830. x.resolveExternals = False
  831. If (x.parseError.errorCode <> 0) Then
  832. ErrRaise "TransformXML", "XML Parse error: " & x.parseError.reason
  833. EXIT FUNCTION
  834. End If
  835. 'Load XSL
  836. Dim xsl
  837. set xsl = CreateObject("MSXML2.DOMDocument")
  838. xsl.async = false
  839. xsl.LoadXML(xslt)
  840. If (xsl.parseError.errorCode <> 0) Then
  841. ErrRaise "TransformXML", "XSL Parse error: " & xsl.parseError.reason
  842. EXIT FUNCTION
  843. End If
  844. 'Transform file
  845. TransformXML = (x.transformNode(xsl))
  846. END FUNCTION
  847. 'get the ebay xml api response
  848. Public FUNCTION GeteBayRSS(eBayVerb, eBayToken, eBayParam1, ebaySiteId, bProduction)
  849. ' eBayVerb: GetSearchResults | GetSellerList | GetCategoryListings
  850. ' eBayToken: <a rel="nofollow" class="t" href="http://developer.ebay.com/tokentool/Credentials.aspx" target="_blank">http://developer.ebay.com/tokentool/Credentials.aspx</a>
  851. ' eBayParam1: Search query, Seller Id or Category Id
  852. ' ebaySiteId: ebay SiteId
  853. ' bProduction: Production or Sandbox
  854. If Trim(eBayVerb) = "" Then
  855. ErrRaise "GeteBayRSS", "eBayVerb must be set"
  856. Exit Function
  857. End if
  858. If Trim(eBayToken) = "" Then
  859. ErrRaise "GeteBayRSS", "eBayToken must be set"
  860. Exit Function
  861. End if
  862. If Trim(ebaySiteId) = "" Then
  863. ebaySiteId = "0"
  864. End if
  865. bProduction = (bProduction=True)
  866.  
  867. Headers.RemoveAll()
  868. Headers.Add "X-EBAY-API-COMPATIBILITY-LEVEL", "305"
  869. Headers.Add "X-EBAY-API-DETAIL-LEVEL", "0"
  870. Headers.Add "X-EBAY-API-CALL-NAME", eBayVerb
  871. Headers.Add "X-EBAY-API-SITEID", ebaySiteId
  872.  
  873. If (bProduction) then
  874. xml_URL = eBayAPIURL
  875. Else
  876. xml_URL = eBayAPISandboxURL
  877. End If
  878. xml_data = eBayCreateRequestXML(eBayVerb, eBayToken, eBayParam1, ebaySiteId, iMaxResults)
  879.  
  880. GeteBayRSS = GetRSS()
  881. END FUNCTION
  882. 'construct the ebay soap request xml
  883. Private FUNCTION eBayCreateRequestXML(UserVerb, UserToken, qry, SiteId, UserMaxResults)
  884. Dim xml
  885. xml = ""
  886. xml = xml & "<?xml version=""1.0"" encoding=""iso-8859-1""?>" & VbCrLf
  887. xml = xml & "<request xmlns=""urn:eBayAPIschema"">"
  888. xml = xml & "<RequestToken>" & UserToken & "</RequestToken>" & VbCrLf
  889. xml = xml & "<SiteId>" & SiteId & "</SiteId>" & VbCrLf
  890. xml = xml & "<DetailLevel>0</DetailLevel>" & VbCrLf
  891. xml = xml & "<ErrorLevel>1</ErrorLevel>" & VbCrLf
  892. xml = xml & "<MaxResults>" & UserMaxResults & "</MaxResults>" & VbCrLf
  893. xml = xml & "<Verb>" & UserVerb & "</Verb>" & VbCrLf
  894. SELECT Case LCASE(UserVerb)
  895. Case "getsearchresults":
  896. xml = xml & "<Query>" & qry & "</Query>" & VbCrLf
  897. Case "getsellerlist":
  898. xml = xml & "<UserId>" & qry & "</UserId>" & VbCrLf
  899. xml = xml & "<ItemsPerPage>" & UserMaxResults & "</ItemsPerPage>" & VbCrLf
  900. xml = xml & "<PageNumber>1</PageNumber>" & VbCrLf
  901. xml = xml & "<EndTimeFrom>2002-01-01 00:00:01</EndTimeFrom>" & VbCrLf
  902. xml = xml & "<EndTimeTo>2020-01-01 00:00:01</EndTimeTo>" & VbCrLf
  903. Case "getcategorylistings":
  904. xml = xml & "<CategoryId>" & qry & "</CategoryId>" & VbCrLf
  905. END SELECT
  906. xml = xml & "</request>" & VbCrLf
  907. eBayCreateRequestXML = Trim(xml)
  908. END FUNCTION
  909. Public FUNCTION eBayTimeLeft(eBayEndTime)
  910. Dim eBayOfficialTime
  911. eBayOfficialTime = eBayTime
  912. If eBayOfficialTime="" Then Exit Function
  913. eBayOfficialTime = Replace(eBayOfficialTime, "GMT", "")
  914. eBayEndTime = Replace(eBayEndTime, "GMT", "")
  915. Dim TimeLeft, TimeLeftD, TimeLeftH, TimeLeftM
  916. TimeLeft = DateDiff("n", eBayOfficialTime, eBayEndTime)
  917. If TimeLeft<0 Then
  918. eBayTimeLeft = "Ended "
  919. Else
  920. TimeLeftD = Int(TimeLeft/( 60 * 24))
  921. TimeLeftH = Int((TimeLeft - (TimeLeftD * 60 * 24)) / 60)
  922. TimeLeftM = Int(TimeLeft - (TimeLeftD * 60 * 24) - (TimeLeftH * 60) )
  923. eBayTimeLeft = TimeLeftD & "d " & TimeLeftH & "h " & TimeLeftM & "m "
  924. End If
  925. END FUNCTION
  926. Private FUNCTION eBayCurrencySymbolFromID(sym)
  927. Dim res, s
  928. res= ""
  929. s = trim(Sym)
  930. If (s= "") Then Exit FUNCTION
  931. If Not IsNumeric(s) Then Exit FUNCTION
  932. s = CLNG(s)
  933.  
  934. SELECT CASE (S)
  935. case 1: res="$"
  936. case 2: res="C $"
  937. case 3: res="GBP"
  938. case 5: res="AU $"
  939. case 7: res="EUR"
  940. case 8: res="FRF"
  941. case 31: res="NLG"
  942. case 13: res="CHF"
  943. case 41: res="NT $"
  944. END SELECT
  945. eBayCurrencySymbolFromID = Trim(res)
  946. END FUNCTION
  947. End Class
  948. %>

Here is the examle how to use that class.
  1. <%= getXML("file.xml","file.xsd") %>

file.xsd is the style in which the xhm should be transformed to html.

This class also deals with Ebay API.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC