Link to home
Start Free TrialLog in
Avatar of quest_capital
quest_capital

asked on

Object required error while parsing XML

I get the error
Object required: 'item(...)'
What is wrong?

Here is the code:
<%
Response.Buffer = True
Dim objXMLHTTP, xml, xmlFile, xmlLocation, xmlDir, elements, i
xmlFile = "http://some_domain/pride/includes/article_index_4.xml"
NumberOfLinks = 3

Dim objXML
Set objXML = Server.CreateObject("microsoft.XMLDOM")

call objXML.setProperty("ServerHTTPRequest", True)
call objXML.Load(xmlFile)

Set elements = objXML.getElementsByTagName("item")  

For i = 1 to NumberOfLinks
tTitle = elements.item(i).childNodes(0).Text '''''''''''''''''''' The Error Line '''''''''''''''''''''''''''''''
Next

Set objXML = Nothing
%>

The XML Code
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
  <channel>
    <title>My company RSS Feed</title>
    <link>http://www.mysite.com/</link>
    <description>My company extends products using XML</description>
    <language>en-uk</language>
    <pubDate>Fri, 16 Apr 2006 15:15:00 +0200</pubDate>
    <item>
      <title>New company started</title>
      <link>http://www.mysite.com/news.asp?id=26</link>
      <comments>http://www.mysite.com/comments.asp?id=26</comments>
      <description>Here the description goes</description>
      <pubDate>Fri, 16 Feb 2006 00:00:00 +0200</pubDate>
    </item>
      <item>
      <title>New company2 started</title>
      <link>http://www.mysite.com/news.asp?id=26</link>
      <comments>http://www.mysite.com/comments.asp?id=26</comments>
      <description>Here2 the description goes</description>
      <pubDate>Fri, 16 Feb 2006 00:00:00 +0200</pubDate>
    </item>
      <item>
      <title>New company3 started</title>
      <link>http://www.mysite.com/news.asp?id=26</link>
      <comments>http://www.mysite.com/comments.asp?id=26</comments>
      <description>Here3 the description goes</description>
      <pubDate>Fri, 16 Feb 2006 00:00:00 +0200</pubDate>
    </item>
  </channel>
</rss>
Avatar of kevp75
kevp75
Flag of United States of America image

here's something that may help.

Function RSSReader(url, numItems, showAll)
      URLToRSS = url
      MaxNumberOfItems = numItems
      MainTemplateHeader = "<table width='100%' cellpadding='0' cellspacing='0'>"
      MainTemplateFooter = "</table>"
      ItemTemplate = "<tr><td style='border-bottom:1px solid #EEE;'><a href=" & """{LINK}""" & " target='_blank'>{TITLE}</a>{DESCRIPTION}{pubDate}{Author}</td></tr>"
      ErrorMessage = "Error has occured while trying to process " &URLToRSS & "<BR>Please contact web-master"
      Set xmlHttp = Server.CreateObject("MSXML2.XMLHTTP.3.0")
            xmlHttp.Open "Get", URLToRSS, false
            xmlHttp.Send()
            RSSXML = xmlHttp.ResponseText
            Set xmlDOM = Server.CreateObject("MSXML2.DomDocument.3.0")
                  xmlDOM.async = false
                  xmlDOM.LoadXml(RSSXML)
            Set xmlHttp = Nothing ' clear HTTP object
            Set RSSItems = xmlDOM.getElementsByTagName("item") ' collect all "items" from downloaded RSS
      Set xmlDOM = Nothing ' clear XML
      RSSItemsCount = RSSItems.Length-1
      ' writing Header
      if RSSItemsCount > 0 then
            Response.Write MainTemplateHeader
      End If
      j = -1
      For i = 0 To RSSItemsCount
            Set RSSItem = RSSItems.Item(i)
            for each child in RSSItem.childNodes
                  Select case lcase(child.nodeName)
                        case "title"
                              RSStitle = child.text
                        case "link"
                              RSSlink = child.text
                        case "description"
                              RSSdescription = child.text
                        case "pubdate"
                              RSSPubDate = child.text
                        case "author"
                              RSSAuthor = child.text
                  End Select
            next
            j = J+1
            if J < MaxNumberOfItems then
                  ItemContent = Replace(ItemTemplate,"{LINK}",RSSlink)
                  if showAll then
                        ItemContent = Replace(ItemContent,"{TITLE}","<div style='padding-left:10px;font-weight:bold;'>"&RSSTitle&"</div>")
                        ItemContent = Replace(ItemContent,"{pubDate}","<div style='font-size:85%;font-weight:bold;font-style:italic;text-align:right;'>Published: "&RSSPubDate&"</div>")
                        ItemContent = Replace(ItemContent,"{Author}","<div style='font-size:85%;font-weight:bold;font-style:italic;text-align:right;'>"&RSSAuthor&"<div align='right' style='padding-right:10px;'><a href='javascript:window.scrollTo(0,0);'><img src='/storage/images/16x_top.gif' alt='Return To The Top Of The Page' border='0' /></a></div></div>")
                        ItemContent = Replace(ItemContent,"{DESCRIPTION}","<div style='font-size:85%;padding:0 10px 0 25px;'>"&RSSDescription&"</div>")
                  else
                        ItemContent = Replace(ItemContent,"{TITLE}","<div style='padding-left:10px;'>"&RSSTitle&"</div>")
                        ItemContent = Replace(ItemContent,"{pubDate}","")
                        ItemContent = Replace(ItemContent,"{Author}","")
                        ItemContent = Replace(ItemContent,"{DESCRIPTION}","")
                  end if
                  write(ItemContent)
                  ItemContent = ""
            End if
      Next
      ' writing Footer
      if RSSItemsCount > 0 then
            Response.Write MainTemplateFooter
      else
            Response.Write ErrorMessage
      End If
End Function


to use it simply:
<%Call RSSReader("http://hosted.ap.org/lineups/WORLDHEADS-rss_2.0.xml?SITE=RANDOM&SECTION=HOME", 5, False)%>

5 is the number of articles I want to display
False will display the titles only (change it to true for the whole article)
Avatar of Carl Tawn
The collection of nodes is zero based, so your code should read:

    For i = 0 to NumberOfLinks - 1
       tTitle = elements.item(i).childNodes(0).Text '''''''''''''''''''' The Error Line '''''''''''''''''''''''''''''''
    Next
Avatar of quest_capital
quest_capital

ASKER

carl_tawn
same error
have you tried my method?

and I believe they are child.nodeName
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial