Click here to Skip to main content
Click here to Skip to main content

Creating An XML Newsfeed

, 29 Jul 2002
Rate this:
Please Sign up or sign in to vote.
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 :

<%

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:

<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

Share

About the Author

CodeToad.Com

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 PinsussAnonymous30-Jul-02 3:30 
AnswerRe: What about web components PinmemberCodeToad.Com30-Jul-02 4:21 
AnswerRe: What about web components PinmemberRyan Johnston30-Jul-02 10:45 
Anonymous wrote:
extract automatically news components from html web pages ?
 
I'm guessing you have never actually tried to do this.
 
It is full of problems. Despite its questionable legality, once you have successfully figured out how to parse out a particluar site's conent, you can't rely on it. They can change the layout/design of their website at any time, and it can break your feed. Unless you are willing to constantly upgrade the way you extract the content, your feed probably wont last long.
GeneralRe: What about web components PinmemberSoliant30-Jul-02 12:26 
GeneralRe: What about web components PinsussAnonymous30-Jul-02 21:15 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140826.1 | Last Updated 30 Jul 2002
Article Copyright 2002 by CodeToad.Com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid