Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / XML
Article

Creating An XML Newsfeed

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
29 Jul 20021 min read 79.3K   20   5
This article explains creating an XML newsfeed in an ASP page.

Introduction

Creating an XML newsfeed is a great way to get to grips with a real world use of XML. This tutorial, taken from CodeToad.Com will explain how to deal with the XML feeds offered by various sites on the web at the moment, and turn them into a useable web page. We will use a style sheet and a little ASP to do the job.

Click here to see how the final newsfeed page will look.

A full list of XML newsfeeds from moreover.com is available at http://w.moreover.com/categories/category_list_xml.html%20target=_blank. Amazon affiliates are also offered the chance to create their links in XML which increases the power of searches, and product placements.

We're going to use the xmlHTTP object within an ASP page to grab the XML from the URL provided by the XML-generating web site. For more information on xmlHttp see my XML HTTP Tutorial.

Once we have the XML, we call the style sheet to display the page.

Here's how the ASP page looks then :

VBScript
<%

dim objHTTP , objXML , objXSL
set objHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objHTTP.open "GET", 
      "http://p.moreover.com/cgi-local/page?c=Pop%20music%20reviews&o=xml", 
      false
objHTTP.send
set objXML = objHTTP.responseXML
set objXSL=Server.CreateObject("microsoft.xmldom")
objXSL.async=false

objXSL.load(Server.MapPath("style.xsl"))

if (objXSL.parseError.errorCode = 0) then
    Response.Write(objXML.transformnode(objXSL))
else
    Response.Write "Error: " & objXSL.parseError.reason 
                     & "<br> URL:" & objXSL.url
end if

Set objHTTP = Nothing
Set objXML = Nothing
Set objXSL = Nothing
%>

We've referenced a style sheet called style.xsl. Our XSL style sheet loops through each item in the XML and displays the record. Obviously, you'll need to look at the XML that you have and adapt the style sheet as necessary, but this should be enough to give you an idea:

XML
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
    <xsl:template match="/">

<html>
<head>
<TITLE>moreover...</TITLE>
</head>
<body BGCOLOR="ffffff">

 <br/><br/>
<br/> br

 

<DIV ALIGN="center">
<TABLE BGCOLOR="ffffff" BORDER="0" CELLPADDING="4" 
                             CELLSPACING="0" WIDTH="100%">

      <xsl:for-each select="moreovernews/article">
       
         <TR VALIGN="middle">
           <TD ALIGN="left" BGCOLOR="ffffff">
             <B>
                   <A>
             <xsl:attribute name="HREF">
               <xsl:value-of select="url"/>
             </xsl:attribute>
             <xsl:attribute name="TARGET">
               _blank
             </xsl:attribute>
             <FONT FACE="Verdana, Arial,Helvetica, 
                       sans-serif" SIZE="-1" COLOR="000000">
               <xsl:value-of select="headline_text"/>
             </FONT>
           </A>
         </B>
         <BR/>
                 <A>
           <xsl:attribute name="HREF">
             <xsl:value-of select="document_url"/>
           </xsl:attribute>
           <xsl:attribute name="TARGET">
             _blank
           </xsl:attribute>
           <FONT FACE="Verdana, Arial,Helvetica, 
                   sans-serif" SIZE="-2" COLOR="666666">
             <xsl:value-of select="source"/>
           </FONT>
         </A>
                 <A>
           <xsl:attribute name="HREF">
           <xsl:value-of select="access_registration"/>
           </xsl:attribute>
           <xsl:attribute name="TARGET">
             _blank
           </xsl:attribute>
           <FONT FACE="Verdana, Arial,Helvetica, 
                  sans-serif" SIZE="-2" COLOR="666666">
             <xsl:value-of select="access_status"/>
           </FONT>
         </A>
         <FONT FACE="Verdana, Arial,Helvetica, 
                 sans-serif" SIZE="-2" COLOR="666666">
           <xsl:value-of select="harvest_time"/> GMT
         </FONT>
           </TD>
         </TR>
          
       </xsl:for-each>


</TABLE>
</DIV>
</body>
</html>
    </xsl:template>
  </xsl:stylesheet>

Here's what the resulting page looks like. Remember we point to the original ASP page to do the work and pull in both the XML and the Style Sheet. Our newsfeed pulled in pop music news, so Click here for the latest popular music news.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This script first appeared on CodeToad.Com, a developers resource of free scripts and tutorials for ASP, ASP.NET, XML, VB, DHTML, Java and other languages.

Comments and Discussions

 
QuestionWhat about web components Pin
Anonymous30-Jul-02 3:30
Anonymous30-Jul-02 3:30 
AnswerRe: What about web components Pin
CodeToad.Com30-Jul-02 4:21
CodeToad.Com30-Jul-02 4:21 
AnswerRe: What about web components Pin
Ryan Johnston30-Jul-02 10:45
Ryan Johnston30-Jul-02 10:45 
GeneralRe: What about web components Pin
TigerNinja_30-Jul-02 12:26
TigerNinja_30-Jul-02 12:26 
GeneralRe: What about web components Pin
Anonymous30-Jul-02 21:15
Anonymous30-Jul-02 21:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.